here i want to append the product id of new product in the cookie name cart so can anyone help me with it
protected void lnkAddToCart_Click(object sender, EventArgs e)
{
HttpCookie CartCookie=Request.Cookies["cart"];
if (CartCookie != null)
{
string str = CartCookie.ToString();
str= str + ";"+ _ProductID.ToString();
Response.Cookies["cart"].Value = str;
}
else
{
CartCookie = new HttpCookie("cart");
CartCookie["Cart"] = _ProductID.ToString();
CartCookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(CartCookie);
}
}
To Set Cookie
public void AddToCartCookie(List<string> listCookie)
{
string objCartListString = string.Join(",", listCookie);
if (Request.Cookies["CartCookie"] == null)
Response.Cookies["CartCookie"].Value = objCartListString;
else
Response.Cookies["CartCookie"].Value = Request.Cookies["CartCookie"].Value + "|" + objCartListString;
Response.Cookies["CartCookie"].Expires = DateTime.Now.AddYears(30);
}
Here listCookie is list of string like string productName, quantity, price etc;
Then retrieve it by splitting like
if (Request.Cookies["CartCookie"] != null)
{
string objCartListString = Request.Cookies["CartCookie"].Value.ToString();
string[] objCartListStringSplit = objCartListString.Split('|');
foreach(string s in objCartListStringSplit)
{
string[] ss = s.Split(',');
productName = ss[0];
quantity = Convert.ToDouble(ss[1]);
price = Convert.ToDecimal(ss[3]);
.........
}
}