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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.