SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry how to store information inside cookies?

    Hi
    I am trying to pass information between two asp.net page. user select item from check list box and calculate how many time add to cart. could any one told me what is problem with my code?
    thank you

    Code:
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                
    
                if(itemListchk.SelectedItem != null)
                {
                    
                    
                    int totalNumberItems = 0;
                    
                    foreach (ListItem li in itemListchk.Items)
                    {
    
                        
                        if (li.Selected == true)
                        {
                            string itemName = itemListchk.SelectedItem.ToString();
                            string itemValue = itemListchk.SelectedValue.ToString();
                            HttpCookie cookie = new HttpCookie(itemName, itemValue);
                            totalNumberItems++;
                            totalNumberItem += Convert.ToInt16(li.Value);
                            
                            Response.Cookies.Add(cookie);
                            
                        }
                        Label1.Text = "You have \t" + totalNumberItems.ToString() + "\t items in your cart ";
                    }
                    
                }
            }

  2. #2
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once i did the program of Cookies but i have done it using JSP and Servlet :
    in Jsp there is one function - getComment() to retrieve all the data stored inside the cookies

  3. #3
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NorthStare View Post
    Hi
    I am trying to pass information between two asp.net page. user select item from check list box and calculate how many time add to cart. could any one told me what is problem with my code?
    thank you

    Code:
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                
    
               
                {
                    
                    
                    int totalNumberItems = 0;
                    
                    foreach (ListItem li in itemListchk.Items)
                    {
    
                        
                        if (li.Selected == true)
                        {
                            string itemName = itemListchk.SelectedItem.ToString();
                            string itemValue = itemListchk.SelectedValue.ToString();
                            HttpCookie cookie = new HttpCookie(itemName, itemValue);
                            totalNumberItems++;
                            totalNumberItem += Convert.ToInt16(li.Value);
                            
                            Response.Cookies.Add(cookie);
                            
                        }
                        Label1.Text = "You have \t" + totalNumberItems.ToString() + "\t items in your cart ";
                    }
                    
                }
            }
    if(itemListchk.Checked)

    I assume you are referring to a checkbox....
    Chuck Norris is so tough,
    mosquitos ask for permission before they bite him

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You really don't want to store information in cookies for a few reason:

    1) Cookies are expensive -- the entire contents are sent both ways with every request.
    2) Cookies are hackable -- unless you are encrypting things in your application everything is sent in the clear.

    A much better option for ASP.NET is to use the User Profile, it is baked in and can be made to work with anonomous users as well. If you are rolling your own solution, the better method is to just use an identifier and store the data in a database.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •