Handle division by zero in linq query

Hi EE,

I have a linq query that calls a function that does some calculations.
I am getting an error because some of the values in the db are 0.
What is the best way to handle this?  Please provide example.


Thanks
Private Sub FillGrid()
      Dim dc As New SalonDataClassesDataContext
      Dim u As New SalonUtilities
      Dim sUserID As String = u.CheckSessionAndReturnUser
      Me.hiOrderID.Value = "0"

      Dim iSupplierID As Integer = Me.ddSupplier.SelectedValue

      Dim q = (From p In dc.ProductDetails Where p.ProductsLU.UserID.ToString = sUserID AndAlso _
             p.ProductsLU.SupplierID = iSupplierID AndAlso p.OuncesInStock <= p.ReorderLevelOunces _
             Select p.ProductDetailID, p.ProductsLU.ProductName, _
             p.ProductsLU.ProductTypeID, p.ItemNumb, p.ProductsLU.ProductTypesLU.ProductType, _
             TotalQty = dc.fn_ReturnInvOrderAmt(p.PreferredAmtInStock, p.OuncesPerItem, p.OuncesInStock), _
              p.OrderPricePerItem, p.LUProdSellUseID, _
              LineTotal = p.TotalQty * p.OrderPricePerItem, _
               Order = 0, OrderDetailID = 0)

      Me.lstProdOrdering.DataSource = q
      Me.lstProdOrdering.DataBind()
   End Sub



####sql server function

ALTER FUNCTION [dbo].[fn_ReturnInvOrderAmt]
(
	-- Add the parameters for the function here
	@PreferredAmtInStock decimal(18,2),
	@OuncesPerItem decimal(18,2),
	@OuncesInStock decimal(18,2)
)
RETURNS int
AS
BEGIN

declare @decAmt decimal(18,2)
Declare @iAmt int

	set @decamt =(@PreferredAmtInStock / @OuncesPerItem) - (@OuncesInStock / @OuncesPerItem)
	return Ceiling(@decamt)
	

END

Open in new window

SheritlwAsked:
Who is Participating?
 
SheritlwAuthor Commented:
It's an inventory table and sometimes there are 0 items.
I have solved the problem.
In my function I checked for 0.

ALTER FUNCTION [dbo].[fn_ReturnInvOrderAmt]
(
      -- Add the parameters for the function here
      @PreferredAmtInStock decimal(18,2),
      @OuncesPerItem decimal(18,2),
      @OuncesInStock decimal(18,2)
)

Thanks for your response.  I will award you the points.
RETURNS int
AS
BEGIN

declare @decAmt decimal(18,2)
Declare @iAmt int
if @OuncesPerItem = 0
   Begin
   set @OuncesPerItem = 1
   end

      set @decamt =(@PreferredAmtInStock / @OuncesPerItem) - (@OuncesInStock / @OuncesPerItem)
      return Ceiling(@decamt)
      

END
0
 
käµfm³d 👽Commented:
I am getting an error because some of the values in the db are 0.
What is the best way to handle this?

My question is why do you have zero values to begin with? Is there some default value you can assume whenever you encounter a zero? 1 perhaps?
0
 
bitrefCommented:
You may replace:
	set @decamt =(@PreferredAmtInStock / @OuncesPerItem) - (@OuncesInStock / @OuncesPerItem)

Open in new window


by:
If (@OuncesPerItem = 0)
	Set @decamt = 999999999999999999.99
Else
	Set @decamt =(@PreferredAmtInStock / @OuncesPerItem) - (@OuncesInStock / @OuncesPerItem)

Open in new window

0
 
SheritlwAuthor Commented:
Found and posted solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.