hemant Rawat

hemant Rawat

  • NA
  • 33
  • 22.6k

Cookies are getting added up when we perform delete oprtn

Feb 12 2018 9:05 AM
asp.net c#
code for cart 
when we are regenrating the cookies there is duplication is adding. inside the cookies 
 
code:
string s, t;
string[] a = new string[6];
int id;
string fname, fcost, cname,fid, qty;
int b,count = 0;
protected void Page_Load(object sender, EventArgs e)
{
id = Convert.ToInt32(Request.QueryString["id"].ToString());
DataTable dt = new DataTable();
dt.Rows.Clear();
dt.Columns.AddRange(new DataColumn[7] { new DataColumn("fname"), new DataColumn("fcost"), new DataColumn("fid"), new DataColumn("qty"), new DataColumn("cname"),new DataColumn("subtotal"),new DataColumn("id")});
if (Request.Cookies["aa"] != null)
{
s = Convert.ToString(Request.Cookies["aa"].Value);
string[] strarr = s.Split('|');
for (int i = 0; i < strarr.Length; i++)
{
t = Convert.ToString(strarr[i].ToString());
string[] strarr1 = t.Split(',');
for (int j = 0; j < strarr1.Length; j++)
{
a[j] = strarr1[j].ToString();
}
dt.Rows.Add(a[0].ToString(), a[1].ToString(), a[2].ToString(), a[3].ToString(), a[4].ToString(), (Convert.ToInt32(a[1].ToString()) * Convert.ToInt32(a[3].ToString())), i.ToString());
}
}
// dt.Rows.RemoveAt(id);
dt.Rows[id].Delete();
b=dt.Rows.Count;
//Response.Write(current);
Response.Cookies["aa"].Expires = DateTime.Now.AddDays(-1);
Response.Write(dt.Rows.ToString());
foreach (DataRow dr in dt.Rows)
{
fname = dr["fname"].ToString();
fcost = dr["fcost"].ToString();
cname = dr["cname"].ToString();
fid = dr["fid"].ToString();
qty = dr["qty"].ToString();
count = count + 1;
if (count == 1)    // here the problem is occuring
{
Response.Cookies["aa"].Value = fname.ToString() + "," + fcost.ToString() + "," + fid.ToString() + "," + qty.ToString() + "," + cname.ToString();
Response.Cookies["aa"].Expires = DateTime.Now.AddHours(1);
}
else if(count>1 && count<=b)
{
Response.Cookies["aa"].Value = Request.Cookies["aa"].Value + "|" + fname.ToString() + "," + fcost.ToString() + "," + fid.ToString() + "," + qty.ToString() + "," + cname.ToString();
Response.Cookies["aa"].Expires = DateTime.Now.AddHours(1);
}
}
Response.Redirect("cart.aspx");

Answers (1)