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

  2. Hi,

    Try this:

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

    Good luck! Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "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
    #2
    1. Advertising

  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


    "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
    #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?

    "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
    #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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GeorgeF

    Perl Shopping Cart Scripts

    GeorgeF, May 21, 2005, in forum: Perl
    Replies:
    1
    Views:
    3,283
    James T
    May 22, 2005
  2. Bruce W...1

    Secure shopping cart example?

    Bruce W...1, Aug 26, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    627
    Bruce W...1
    Aug 27, 2003
  3. Mark Fox

    Shopping Cart Component Suggestions

    Mark Fox, Nov 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    393
    Jacob Yang [MSFT]
    Nov 4, 2003
  4. John
    Replies:
    1
    Views:
    376
    George Shubin
    Nov 7, 2003
  5. Paul Hobbs

    Shopping Cart - database vs session

    Paul Hobbs, Dec 4, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,820
    Jaxon
    Dec 4, 2003
Loading...

Share This Page