Calculating a Subtotal for Shopping Cart

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

  1. 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
    #1
    1. Advertisements

  2. Hi,

    Try this:

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

    Good luck! Ken.
     
    Ken Dopierala Jr., Mar 6, 2005
    #2
    1. Advertisements

  3. Sparky Arbuckle

    gaidar Guest

    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
     
    gaidar, Mar 6, 2005
    #3
  4. 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
    #4
  5. 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
    #5
  6. Sparky Arbuckle

    gaidar Guest

    May be you will post complete code of the class?
     
    gaidar, Mar 6, 2005
    #6
  7. 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
     
    Sparky Arbuckle, Mar 6, 2005
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.