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
E-CommerceVisual Basic.NET

Avatar of undefined
Last Comment
tolgaong
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo