locked
Add values to existing cookie RRS feed

  • Question

  • User-2041915856 posted

    Hi all, I am facing the following task:

    As the user navigates the site logs onto the website, write the
    following information

    •  Current Date
    •  Current Time
    •  Name of up to 5 Products the user viewed or Ordered during the visit

    As I don't really have a product list site, I was going to do on the order screen. When user inputs the quantity to order I have a TextChanged event. So I can get details of a single product with the required information, but I am not sure how to keep adding to it? 

    This is what I am doing atm:

    foreach (GridViewRow row in this.GridView1.Rows)
            {
             //   string pName1 = GridView1.DataKeys[test].Values["ProductName"].ToString();
                testCookie.Values.Add("CurrentDate", currentDate);
                testCookie.Values.Add("CurrentTime", currentTime);
                testCookie.Values.Add("ProductName", pName);
                Label lblPartStatus = ((Label)row.Cells[4].FindControl("Label8"));
                double testprice = Convert.ToDouble(lblPartStatus.Text);
                grandTotal = grandTotal + testprice;
            }

    And also, how do I limit/check the amount of products that have been saved in the cookie and replace the oldest one with a newer ones?

    Thanks for any advise!

    Tuesday, November 3, 2015 3:13 AM

Answers

  • User-986267747 posted

    Hi Artlemaks,

    but I am not sure how to keep adding to it? 

    how do I limit/check the amount of products that have been saved in the cookie and replace the oldest one with a newer ones?

    Based on your needs, I suggest that you use the session to store information, because the cookie can store content too little, and can not directly store an entity class.

    First, you can create a complex type to store information.

        public class Product 
        {
            public string productName { get; set; }
        }
    
        public class VisitInformation
        {   
            public DateTime currentDate { get; set; }
    
            public Product productInformation { get; set; } 
        }

    If you want to get the current date and time, you could refer to the following link and use the currentDate to get these information.

    http://stackoverflow.com/questions/296920/how-do-you-get-the-current-time-of-day

    http://stackoverflow.com/questions/6817266/get-current-date-only-in-c-sharp

    Next, you need to add the Visit Information when the user viewed or Ordered during the visit. Like this.

                object testSession = Session["VisitInformationList"];
                if (testSession == null)
                {
                    VisitInformation addInformation = new VisitInformation();
                    addInformation.currentDate = DateTime.Now;
                    addInformation.productInformation = new Product { productName = "test1" };
                    List<VisitInformation> list = new List<VisitInformation>();
                    list.Add(addInformation);
                    Session.Add("VisitInformationList", list);
                }
                else
                {
                    List<VisitInformation> informationList = (List<VisitInformation>)Session["VisitInformationList"];
    // limit/check the amount of products, and take the recently 4 products if (informationList.Count >= 5) { informationList = informationList.OrderBy(x => x.currentDate).Take(4).ToList(); } VisitInformation addInformation = new VisitInformation(); addInformation.currentDate = DateTime.Now; addInformation.productInformation = new Product { productName = "test2" }; informationList.Add(addInformation); Session.Add("VisitInformationList", informationList); }

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 3, 2015 9:23 PM