Cookie not created! Help please.

  • Thread starter Thread starter Clint
  • Start date Start date
C

Clint

I am using the following C# code in a function called by a
button click:

HttpCookie cookie = new HttpCookie("UserName");
cookie.Value = tbUserName.Text;
Response.Cookies.Add(cookie);

When I go to look for this cookie on my system, I cannot
find it anywhere. If I close my browser, then open the
browser again and try to reload the page, I get ASP.NET
errors because of a null reference when the cookie was not
found. Obviously, I need to add some code to check if the
cookie exists and if not load the page with defaults, but
why is the cookie not created? Am I missing something? I
am testing this on a Windows XP Professional system
running IIS.
 
If you want the cookie to be kept when you close your browser, you must set
the Expires property of the cookie to something; this will make a persistent
cookie.

If you don't do so (like in your example), a temporary cookie is created. A
temporary cookie is lost when the browser is closed, while a persistent
cookie isn't.
 
Back
Top