2 Replies - 10142 Views - Last Post: 13 June 2012 - 02:45 AM

#1 ankur.una  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-March 11

Making a simple Shopping Cart using Cookies or Sessions

Posted 04 June 2012 - 01:33 AM

I am trying to make a simple shopping cart using cookies but I having some problems.

Here is the code for the main page (Default.aspx)

<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>



And here is code behind it, (Default.aspx.cs)

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["Item"] != null)
        {
           ListBox1.SelectedValue = Request.Cookies["Item"].Value;
        }

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie userCookie = new HttpCookie("UserInfo");
        userCookie["userName"] = TextBox1.Text;
        userCookie["email"] = TextBox2.Text;
   
        userCookie["items"] = ListBox1.SelectedValue;
        //Response.Cookies["Username"].Value =TextBox1.Text;
        //Response.Cookies["Email"].Value = TextBox2.Text;
        //Response.Cookies["Items"].Value = ListBox1.SelectedValue;
        Response.Cookies.Add(userCookie);
        Response.Redirect("confirm.aspx");
    }
}



I am trying to select all four items, and in the next page I am trying to display the subtotal but error I am getting is it is displaying only first item repeatedly.

Here is the code for next page (confirm.aspx)

<form id="form1" runat="server">
    <div style="height: 229px">
    
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
    
    </div>
    </form>



and the code for it, (confirm.aspx.cs)

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 suggestion where I might be wrong? The point to note here is, I am total newbie to ASP.NET. One more thing, any tutorials or book from where I can learn ASP.NET better?

Is This A Good Question/Topic? 0
  • +

Replies To: Making a simple Shopping Cart using Cookies or Sessions

#2 ankur.una  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-March 11

Re: Making a simple Shopping Cart using Cookies or Sessions

Posted 04 June 2012 - 05:32 PM

Not a single reply? :( Is there anyone who can help me?
Was This Post Helpful? 0
  • +
  • -

#3 evaaggy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 12-June 12

Re: Making a simple Shopping Cart using Cookies or Sessions

Posted 13 June 2012 - 02:45 AM

you are reading only one value from the listbox and displaying it multiple times.

You need to introduce a loop. Something like this,

for(int i=ListBox1.Items.Count-1;i>=0;i--)
      {
          if (ListBox1.Items[i].Selected==true)
          {
              ;//add ListBox1.Items[i] to cookie
          }
     }


You can check out asp.net with c# tutorials and free code download here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1