Shopping Cart Using Cookies
asp.net, c#
Solution
Is a bad idea to store all the items that the user adds to the cart on cookies because the cookie have limitation on how many data you can store, and second the page carry all that data on every request and you make your page difficult to load.
If you try to save it on the session, then the user may lose whats on the cart when the session expire, and this can happens even at check out. For example, the user starts the check out, is stop for some minutes to do something, and then when its try to continue the session has expires.
The correct way is to keep the cart on a database, connected with the user cookie that have an expiration of six months at least.
about your code
the bug on your code is that you use the same cookie name for all, that why you see only the first. See, this is the same and not changes on the lines.
Request.Cookies["UserInfo"]["items"]
Problem
I am trying to make a simple shopping cart using cookies/sessions. It just contains 4 items according to this code snippet, ``` <form id="form1" runat="server"> <div style="height: 296px"> <asp:ListBox ID="ListBox1" runat="server" Height="164px" Width="107px" SelectionMode="Multiple"> <asp:ListItem>Tyres</asp:ListItem> <asp:ListItem>Battery</asp:ListItem> <asp:ListItem>Front Glass</asp:ListItem> <asp:ListItem>Vanity Mirrors</asp:ListItem> </asp:ListBox> <br /> <br /> Username:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <br /> EMail: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" /> </div> </form> ``` what I am trying is I am selecting all four items and on clicking the button, it will take me to the next page for entering prices and calculating prices and in the other page I will show the total with taxes. I am stuck on the second page as it just shows the First selection not the other three. Below is the code for second page: ``` public partial class confirm : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["UserInfo"] != null) { // TextBox1.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["userName"]); //TextBox2.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["email"]); Label1.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["items"]); Label2.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["items"]); Label3.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["items"]); Label4.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["items"]); } } } ``` Any ideas guys where I might be wrong?