Link to home
Start Free TrialLog in
Avatar of FabRed
FabRed

asked on

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
Avatar of tolgaong
tolgaong
Flag of Türkiye image

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
Avatar of FabRed
FabRed

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of tolgaong
tolgaong
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial