• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

Shopping cart question! Pls help!

Hi.

Can someone pls help me with a little problem I have? I have wrote a code to add items to my shopping cart (code below). What I wish to accomplish is that if the customer adds the same item to the shopping cart twice the system adds one to the items amount in the shopping cart instead of adding a new line in the cart. Is there a simple code I can add to this code to accomplish this?

Dim row As GridViewRow
        Dim isbn As String
        Dim title As String
        Dim amount As Integer
        Dim price As Double
        Dim t As TextBox
        Dim p As Label
        Dim totalprice As Double

        row = GridView1.SelectedRow

        t = CType(row.Cells(8).Controls(1), TextBox)
        amount = CInt(t.Text)

        p = CType(row.Cells(3).Controls(1), Label)
        price = CDbl(p.Text) * amount

        isbn = row.Cells(0).Text
        tittel = row.Cells(1).Text

        If amount = 0 Then
            Server.Transfer(isbn & ".aspx")
        Else
            Dim shoppingcart As New Data.DataTable
            shoppingcart = CType(Session("cart"), Data.DataTable)
            Dim nyrow As Data.DataRow
            nyrow = handlekurv.NewRow
            nyrow(0) = isbn
            nyrow(1) = title
            nyrow(2) = amount
            nyrow(3) = price
            shoppingcart.Rows.Add(nyrow)

            Session("cart") = shoppingcart
            'regner ut totalprisen i handlekurva og skriver denne ut på siden
            totalprice = Session.Item("Totalprice")
            totalprice = totalprice + price
            Session.Add("Totalprice", totalprice)
        End If
0
FabRed
Asked:
FabRed
  • 2
1 Solution
 
tolgaongCommented:
If amount = 0 Then
            Server.Transfer(isbn & ".aspx")
        Else
            Dim shoppingcart As New Data.DataTable
            shoppingcart = CType(Session("cart"), Data.DataTable)
            Dim nyrow As Data.DataRow
            nyrow = handlekurv.NewRow
            nyrow(0) = isbn
            nyrow(1) = title
            nyrow(2) = amount
            nyrow(3) = price

-----------
datarow[] dr = shoppingCart.Select('isbn=' & isbn) => this will return a row if there is already the same isbn
if dr.length = 0 then
            shoppingcart.Rows.Add(nyrow)
else
    'I am not sure but,
dr["amount"] += 1 could be enough. if it is not, find the row and increase the amount.
end if
-----

            Session("cart") = shoppingcart
            'regner ut totalprisen i handlekurva og skriver denne ut på siden
            totalprice = Session.Item("Totalprice")
            totalprice = totalprice + price
            Session.Add("Totalprice", totalprice)
        End If
0
 
FabRedAuthor Commented:
I am very new at this, so I am sorry, but I need a little bit more help here! When I use the code you wrote, I get errors because datarow and dr is not decleared. What should they be declared as?
Thaks.
0
 
tolgaongCommented:
Hi againn. Really really sorry.
I mixed up the csharp syntax and vb syntax together

here is the correct syntax
dim dr() as system.data.datarow = shoppingCart.Select('isbn=' & isbn) => this will return a row if there
'is already the same isbn
if dr.length = 0 then
            shoppingcart.Rows.Add(nyrow)
else
    'I am not sure but,
     dr("amount") += 1 'could be enough. if it is not, find the row and increase the amount.
end if

Try the above code and let me know if it does not solve the problem.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now