The problem during writing into an existing cookie, after re running my web application
hi to all forums members
can any one tell me what is error in mine code, It create every time a new cookie, but i have to write into existing cookie.
When i close my web application and re running my web application and, adding values into cookies it creates new cookies... why I m unable to understand....
following is my code...
protected void btnAddCokkie_Click(object sender, EventArgs e)
{
CreateAndAddCookiesValue();
}
private void CreateAndAddCookiesValue()
{
//Response.Cookies.Remove("ShowHistoryCookie");
if (Request.Browser.Cookies == true)
{
// Check the cookie Mycookie exist or not
if (Request.Cookies["ShowHistoryCookie"] == null)
{
// If does Mycookie does not exist then create the cookie
HttpCookie aShowHistoryCookie = new HttpCookie("ShowHistoryCookie");
Response.Cookies.Add(aShowHistoryCookie);
// set expires to the datetime maxvalue
Response.Cookies["ShowHistoryCookie"].Expires = DateTime.Now.AddMonths(1);
//Response.Cookies["ShowHistoryCookie"].Expires = DateTime.MaxValue; //DateTime.Now.AddMonths(1)
Response.Cookies["ShowHistoryCookie"]["URL"] += txtUrlLink.Text;
}
//Append UrlLink into existing cookie
else
{
string strCookie = Request.Cookies["ShowHistoryCookie"]["URL"].ToString();
string strCook = strCookie;
strCookie += "|" + txtUrlLink.Text;
char[] splitter = { '|' };
string[] arInfo = new string[10];
arInfo = strCook.Split(splitter);
int count = arInfo.Length;
int intResult=-1;
bool flag = false;
//count to Restrict cookie to Store 10 UrlLink
if (count <10)
{
//check to ensure that no duplicacy in UrlLink of cookies
foreach (string str in arInfo)
{
intResult = str.CompareTo(txtUrlLink.Text);
if (intResult == 0)
{
flag = true;
break;
}
}
if (flag==false)
{
Response.Cookies["ShowHistoryCookie"]["URL"] = strCookie;
}
}
//Sorting cookies to ensure that only ten recents Url link are added only...
else if (count >= 10)
{
foreach (string str in arInfo)
{
intResult = str.CompareTo(txtUrlLink.Text);
if (intResult == 0)
{
flag = true;
break;
}
}
if (flag == false)
{
for (int i = 0; i < 10; i++)
{
//Swaping
if (i + 1 >= 10)
{
arInfo[9] = txtUrlLink.Text;
break;
}
else
{
arInfo[i] = arInfo[i + 1];
}
}
strCookie = arInfo[0] + "|" + arInfo[1] + "|" + arInfo[2] + "|" + arInfo[3] + "|" + arInfo[4] + "|" + arInfo[5] + "|" + arInfo[6] + "|" + arInfo[7] + "|" + arInfo

+ "|" + arInfo[9];
Response.Cookies["ShowHistoryCookie"]["URL"] = strCookie;
}
}
}
}
else
{
// Else display message that
// the browser does not accept cookies
Response.Write("This Browser does not accept cookies");
}