Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

System.InvalidCastException error

CREATE PROCEDURE cartTotal
(@CartID char(36), @gift smallint  OUTPUT)

SELECT @gift = SUM(Quantity)
FROM ShoppingCart
WHERE ShoppingCart.CartID = @CartID AND Gift = 1

Gift is a bit field. Quantity an int.
What I want to do is to total the Quantity field for every row where Gift = 1.
Example:
  Gift             Quantity
   1                   1
   0                   1
   1                   3           so in this case @gift would equal 4.

I tried the above but get an error:

Exception Details:
System.InvalidCastException: Cast from type 'DBNull' to type 'Short' is not valid

details.gift = CType(command.Parameters("@gift").Value, Short)

But this line works in above: SELECT @gift = count(*)        . . . but I need to add the
Quantity to the count for every row where Gift = 1.
thanks
ASKER CERTIFIED SOLUTION
Avatar of rafrancisco
rafrancisco

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MikeMCSD

ASKER

thank you . . that worked.