6 Replies - 200 Views - Last Post: 31 January 2018 - 08:44 PM Rate Topic: -----

#1 ikhlas06  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 284
  • Joined: 20-May 16

How to create session when user click on 'add to cart'

Posted 31 January 2018 - 02:41 PM

How to create session when user click on 'add to cart'

I am using vb asp.net

I creating a online shopping website for fun. On shopping page, user can click add to cart button.
When user click this button than I was to save this information in session, so I can display later.
Session information is simiple, just color, size and qty.

Below code works fine, if you click on 'add to cart' button once. If user click again, old session will get deleted.

I have no idea how people solve this issue on websites. any input into this matter will be helpful to me.


Public Sub SubmitB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitB.Click

Dim ColorS as string = ColorDDL.text
Dim SizeS as string = SizeDDL.text
Dim QTYS as string = QTYHF.text

Session(“ColorS”) = ColorS
Session(“SizeS”) = SizeS
Session(“QTYS”) = QTYS

End sub
:code:

This post has been edited by modi123_1: 31 January 2018 - 02:42 PM
Reason for edit:: In the future use the [code] button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: How to create session when user click on 'add to cart'

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13776
  • View blog
  • Posts: 54,971
  • Joined: 12-June 08

Re: How to create session when user click on 'add to cart'

Posted 31 January 2018 - 02:42 PM

Why wouldn't you start the session when the user log in, and end it when they log out?
Was This Post Helpful? 0
  • +
  • -

#3 ikhlas06  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 284
  • Joined: 20-May 16

Re: How to create session when user click on 'add to cart'

Posted 31 January 2018 - 02:47 PM

Becuase user should be able to add to cart without logging in. Maybe big sites do this

User only need to login for checkout. last step

This post has been edited by ikhlas06: 31 January 2018 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13776
  • View blog
  • Posts: 54,971
  • Joined: 12-June 08

Re: How to create session when user click on 'add to cart'

Posted 31 January 2018 - 02:49 PM

Then I would imagine the 'add to cart' would check if a sessions exists. If not create one before adding some item id and quantity to it. If already created then go about the normal business.

You can create some sort of serializable class to store up in that session.. pull it down as needed.. or even squirrel away the info to a database.
Was This Post Helpful? 0
  • +
  • -

#5 ikhlas06  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 284
  • Joined: 20-May 16

Re: How to create session when user click on 'add to cart'

Posted 31 January 2018 - 02:51 PM

if you click on 'add to cart' button once it works fine. If user click again, old session will get deleted.

old session could be user last item.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13776
  • View blog
  • Posts: 54,971
  • Joined: 12-June 08

Re: How to create session when user click on 'add to cart'

Posted 31 January 2018 - 02:58 PM

By the looks of it you mean the session variable values are being overwritten. Right now you are only assigning values. If you want a collection then assign a collection, json, xml, or what ever.
Was This Post Helpful? 0
  • +
  • -

#7 ikhlas06  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 284
  • Joined: 20-May 16

Re: How to create session when user click on 'add to cart'

Posted 31 January 2018 - 08:44 PM

i did more research and turn out cookies will be better options for me

if you click 'add to cart' 1st time then cookie will be:
        Response.Cookies("CartInfo")("ProductIDS") = 1
        Response.Cookies("CartInfo")("ColorS") = red
        Response.Cookies("CartInfo")("SizeS") = black
        Response.Cookies("CartInfo")("QtyS") = 2
        Response.Cookies("CartInfo").Expires = Now.AddDays(1)


now lets say user clicks on 'add to cart' 2nd time.. on different item. bc of this... 1st cookie will be over writed.

        
        Response.Cookies("CartInfo")("ProductIDS") = 2
        Response.Cookies("CartInfo")("ColorS") = yellow
        Response.Cookies("CartInfo")("SizeS") = black
        Response.Cookies("CartInfo")("QtyS") = 1
        Response.Cookies("CartInfo").Expires = Now.AddDays(1)








below is my full code:
Public Sub SubmitB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitB.Click

        Response.Cookies("CartInfo")("ProductIDS") = Product_ID_URL
        Response.Cookies("CartInfo")("ColorS") = ColorS
        Response.Cookies("CartInfo")("SizeS") = SizeS
        Response.Cookies("CartInfo")("QtyS") = QtyS
        Response.Cookies("CartInfo").Expires = Now.AddDays(1)

end sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1