Cookie not created! Help please.

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.
 
A

AW

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top