Hello! I want to create an eshop basket page for my website. In the page of each product there is a button which when I press sets a cookie with the value of the respective product_id, using the following code: <asp:Button ID="btbasket" runat="server" Text="Buy this product" /> In the code behind file, I have the following: Protected Sub insert_cookie(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btbasket.Click Dim MyCookie As New HttpCookie("eshopCookie") MyCookie.Value = MyCookie.Value & product_code Response.Cookies.Add(MyCookie) MyCookie.Expires = DateTime.Now.AddHours(12) End Sub Then, when the user wants to see all the things he/she has ordered, they visit the Basket.aspx page, which has this: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim colCookies As New ArrayList() For i As Integer = 0 To Request.Cookies.Count - 1 colCookies.Add(Request.Cookies(i)) Next grdCookies.DataSource = colCookies grdCookies.DataBind() End Sub and, in the .aspx file, <asp:GridView id="grdCookies" Runat="server"/> But my problem is that the gridview only shows 1 product_id, and, in particular, tha last product the user has selected a he browses the list of the available products... Is there something else I must do to store all selected product_ids in the cookie and then retrieve them in Basket.aspx page? Must I use different cookie each time? Thank you for your time...
The problem is you are inserting a cookie with the same name each time "eshopCookie", so since you are using the same name, the existing cookie value gets replaced by the new data, so at the end you are left with the last item u insterted thru the cookie. Try using differnt names, may be prefix with a number for each new cookie like 1_eshopCookie , 2_eshopCookie and your code should work fine.
Your code isnt picking up the current value of the cookie so you would need to do that to do what you are proposing. The other problem you will have once you have fixed this is that you are going to have a single long string for the product IDs (eg if you had products 12345 and 54321 in the cart it would have a string 1234554321) so you really need to insert some break between them to as well.
If you want to use a more efficient method create a database table and call it basket or something When a user adds items to their cart: Add the product id, qty, date time, Session ID and IP as a new record You would also need to run a script that cleans out all cart items that have been in the cart for more then say 1 hour
Alternatively dont clear them out but have a status field that can be changed. Knowing what people are adding to their basket but not going on to buy is a useful piece of MI
I agree with imvain, store it in a database table and setup a cronjob every xx minutes to clear it out Thank You Cookies
Technically you could use the windows scheduler However, I use this method for my custom shopping carts, and I have include files that are included on every page. That is where I run my cart cleanup CartTimeOut = 2 * (60 * 60) 'get milliseconds CLEAN_DB_SQL= "delete from cart_items where unix_timestamp(now()) - unix_timestamp(dateadded) >= " & CartTimeOut run sql code Code (markup):