# Calculating a Subtotal for Shopping Cart

Discussion in 'ASP .Net' started by Sparky Arbuckle, Mar 6, 2005.

1. ### Sparky ArbuckleGuest

I'm trying to calculate the subtotal for each item the customer has in
the shopping cart. objDataReader("decPrice") is the price from my
database field called decPrice. The .8 is for a 20% discount. The
error I get is:

Compiler Error Message: BC30201: Expression expected.

What is the correct syntax for this operation? When I check the
compiler output there is a ~ underneath the &. Any help would be
greatly appreciated.

<!-- CODE SNIPPET -->

Dim strSubTotal as string = 0
Dim i as integer = 0
FOR i = 0 to gtCart.count -1
dim strItemID as string = ds.Tables("tblSCart").rows(i)("ItemID")
ds.Tables("tblSCart").rows(i)("Qty") = gtCart(strItemID)
strSubTotal = .8 * & objDataReader("decPrice")
NEXT

<!-- CODE SNIPPET -->

Sparky Arbuckle, Mar 6, 2005

2. ### Ken Dopierala Jr.Guest

Hi,

Try this:

Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Sparky Arbuckle" <> wrote in message
news:...
> I'm trying to calculate the subtotal for each item the customer has in
> the shopping cart. objDataReader("decPrice") is the price from my
> database field called decPrice. The .8 is for a 20% discount. The
> error I get is:
>
> Compiler Error Message: BC30201: Expression expected.
>
> What is the correct syntax for this operation? When I check the
> compiler output there is a ~ underneath the &. Any help would be
> greatly appreciated.
>
> <!-- CODE SNIPPET -->
>
> Dim strSubTotal as string = 0
> Dim i as integer = 0
> FOR i = 0 to gtCart.count -1
> dim strItemID as string = ds.Tables("tblSCart").rows(i)("ItemID")
> ds.Tables("tblSCart").rows(i)("Qty") = gtCart(strItemID)
> strSubTotal = .8 * & objDataReader("decPrice")
> NEXT
>
> <!-- CODE SNIPPET -->
>

Ken Dopierala Jr., Mar 6, 2005

3. ### gaidarGuest

The error is here:

strSubTotal = .8 * & objDataReader("decPrice")

If you want to add ".8 *" as string do it in the following way:

strSubTotal = ".8 *" & objDataReader("decPrice")

But if you want to multiply the value of objDataReader("decPrice") by .8 and
then convert the value to string

strSubTotal = CType(0.8 * CType(objDataReader("decPrice"), Double), string)
Best regards,

Gaidar

"Sparky Arbuckle" <> wrote in message
news:...
> I'm trying to calculate the subtotal for each item the customer has in
> the shopping cart. objDataReader("decPrice") is the price from my
> database field called decPrice. The .8 is for a 20% discount. The
> error I get is:
>
> Compiler Error Message: BC30201: Expression expected.
>
> What is the correct syntax for this operation? When I check the
> compiler output there is a ~ underneath the &. Any help would be
> greatly appreciated.
>
> <!-- CODE SNIPPET -->
>
> Dim strSubTotal as string = 0
> Dim i as integer = 0
> FOR i = 0 to gtCart.count -1
> dim strItemID as string = ds.Tables("tblSCart").rows(i)("ItemID")
> ds.Tables("tblSCart").rows(i)("Qty") = gtCart(strItemID)
> strSubTotal = .8 * & objDataReader("decPrice")
> NEXT
>
> <!-- CODE SNIPPET -->
>

gaidar, Mar 6, 2005
4. ### Sparky ArbuckleGuest

Thanks for the prompt reply Ken. Now I get a new error:

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Sparky Arbuckle, Mar 6, 2005
5. ### Sparky ArbuckleGuest

Gaidar:

I get the same error when I try your example. Maybe more insight would
be had if I were to tell the group that I am binding this information
to a dataGrid?

Sparky Arbuckle, Mar 6, 2005
6. ### gaidarGuest

May be you will post complete code of the class?

"Sparky Arbuckle" <> wrote in message
news:...
> Gaidar:
>
> I get the same error when I try your example. Maybe more insight would
> be had if I were to tell the group that I am binding this information
> to a dataGrid?
>

gaidar, Mar 6, 2005
7. ### Sparky ArbuckleGuest

You got it:

'Retrieve querystring parameters
Dim strDelete as string = Request.querystring("Delete")

'If no changes to cookie then return
IF strDelete < " " and strAdd < " " THEN RETURN

IF strDelete > " " THEN
IF gtcart.Contains(strDelete) THEN
IF gtCart(strDelete) = 1 THEN
gtCart.remove(strDelete)
ELSE
gtCart(strDelete) = gtCart(strDelete)-1
END IF
ELSE
Response.Write("No such ASIN in cart: " & strDelete & "<br>")
RETURN
END IF
END IF

IF strAdd > " " THEN
ELSE
END IF
END IF

'Delete cookie if empty. This eliminates empty "ghost" key.
IF gtCart.count = 0 THEN
RETURN
END IF

Dim iCount as integer = 0
Dim objConn as new
OLEDBConnection(ConfigurationSettings.AppSettings("StrConnection"))
Dim strSQL as string = "SELECT ASIN, strTitle, strArtist, decPrice,
strLabel, intNumberDisks, dtReleaseDate, strReview, strImageDir " & _
"FROM tblDescription WHERE "

FOR EACH myItem as DictionaryEntry in gtCart
iCount = iCount + 1

strSQL += "ASIN= '" & myItem.Key & "'"

IF iCount < gtCart.Count
strSQL += " OR "

ELSE

strSQL += ";"
END IF

NEXT

objConn.Open()

Dim objCommand as new OLEDBCommand(strSQL,objConn)

Dim myCommand as new OleDbDataAdapter(strSQL, objConn)
myCommand.Fill(ds, "tblSCart")

Dim strSubTotal as string = 0
Dim i as integer = 0
FOR i = 0 to gtCart.count -1
dim strASIN as string =
ds.Tables("tblSCart").rows(i)("ASIN")
ds.Tables("tblSCart").rows(i)("Qty") = gtCart(strASIN)
strSubTotal = ".8 *" & objDataReader("decPrice")
NEXT

lblShipping.text = 0
lblSubTotal.text = strSubTotal
lblGrandTotal.text = 0

dgSCart.DataSource = ds.Tables("tblScart").DefaultView
dgSCart.DataBind()

FOR EACH myItem as DictionaryEntry in gtCart