Calculating a Subtotal for Shopping Cart

  • Thread starter Sparky Arbuckle
  • Start date
S

Sparky Arbuckle

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

Ken Dopierala Jr.

Hi,

Try this:

strSubTotal = CStr(.8 * CDec(objDataReader("decPrice")))

Good luck! Ken.
 
G

gaidar

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
 
S

Sparky Arbuckle

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

Sparky Arbuckle

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

Sparky Arbuckle

You got it:

Sub UpdateCookie(byRef gtCart as HashTable)

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

'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
IF gtCart.Contains(strAdd) THEN
gtCart(strAdd) = gtcart(strAdd) + 1
ELSE
gtCart(strAdd) = 1
END IF
END IF

'Delete cookie if empty. This eliminates empty "ghost" key.
IF gtCart.count = 0 THEN
Response.Cookies(MyCookieName).Expires = Now.AddDays(-1)
RETURN
END IF

'Write HashTable back to cookie
Dim MyCookie as new httpCookie(MyCookieName)

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
MyCookie.Values(myItem.Key) = myItem.Value

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

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

ELSE

strSQL += ";"
END IF

Response.Cookies("Customer")("Quantity") = MyItem.Value

NEXT

Dim objDataReader as OLEDBDataReader
objConn.Open()

Dim objCommand as new OLEDBCommand(strSQL,objConn)

Dim myCommand as new OleDbDataAdapter(strSQL, objConn)
myCommand.Fill(ds, "tblSCart")
ds.Tables("tblSCart").Columns.Add("Qty")

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

'Write cookie to page
Response.Cookies.Add(MyCookie)
Response.Cookies(MyCookieName).Expires = Now.AddDays(30)

FOR EACH myItem as DictionaryEntry in gtCart
MyCookie.Values(myItem.Key) = myItem.Value

NEXT

END Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top