Sige VV
Sige VV

Reputation: 68

Vb.Net ASP.Net Shopping Cart with cookies

I am building an E-commerce website and everything went great until I started thinking about how I'm going to make my shopping cart, using cookies.

I'm using a datalist to display my products, when a user clicks on a button, I want the product ID to be stored in a cookie so when the user goes to his shopping cart, the cart checks all the ID's in the cookie and then checks in my access database which product it should add.

Does anyone know how I should make this work or have a useful tutorial? Because all the tutorials I found are in java, php, c# etc. but I need to make this in vb.net because it's a school project.

Thanks in advance!

Upvotes: 0

Views: 1076

Answers (2)

Sige VV
Sige VV

Reputation: 68

If Request.Cookies("Winkelmand") Is Nothing Then Dim objCookie As New HttpCookie("Winkelmand")

        objCookie.Values.Add(strProductId, strProductId)

        objCookie.Expires = Now.AddDays(30)

        Response.Cookies.Add(objCookie)
    Else
        Dim objCookie As HttpCookie = Request.Cookies("Winkelmand")
        Dim strControleDubbel As String

        strControleDubbel = objCookie.Values.Item(strProductId)

        If strControleDubbel = Nothing Then
            objCookie.Values.Add(strProductId, strProductId)

            Response.Cookies.Add(objCookie)

        End If

This code worked!

Upvotes: 0

Sige VV
Sige VV

Reputation: 68

Protected Sub Button1_Click(sender As Object, e As EventArgs)

    Dim btnShoppingCart As Button = TryCast(sender, Button)

    'get values from datalist
    Dim datalistItem As DataListItem = DirectCast(btnShoppingCart.NamingContainer, DataListItem)
    'productId
    Dim lblProductId As Label = DirectCast(datalistItem.FindControl("lblProductId"), Label)
    Dim strProductId As String = lblProductId.Text

    Dim objCookie As New HttpCookie("ShoppingCart")

    objCookie.Value = strProductId

    objCookie.Expires = Now.AddDays(30)

    Response.Cookies.Add(objCookie)
End Sub

End Class If I do it like this, I just get one value saved in the cookie, any way to get multiple values?

If I would use objCookie.Values.add("ID", strProductId) I can only save 1 ID to it, right?

Upvotes: 0

Related Questions