Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i set a cookie in asp.net? 1

Status
Not open for further replies.

drew10

Programmer
Feb 26, 2002
123
US
I need a cookie to remember a users login info. I have been trying this:

setting cookie-->
dim userID as new HttpCookie("myUserID")
userID.value = textbox1.text
Response.Cookies.Add(userID)
userID.expires = now.AddDays(30)

getting cookie value-->
textbox1.text = response.cookies("myUserID").value

I know the cookie is created, since I can see it in the cookies folder, but when I visit the page again, the cookie is deleted from the folder, and the value does not appear in textbox1.

Am I doing something wrong? This is the first time I've ever set a cookie in asp.net, so any suggestion would be of great help.

Thanx,
drew10
 
Hi
When you set the cookie, you might try alternating the order of the lines - try the following order

setting cookie-->
dim userID as new HttpCookie("myUserID")
userID.value = textbox1.text
userID.expires = now.AddDays(30)
Response.Cookies.Add(userID)

When you retrieve the cookie, you need to access the request object, not the response object. Try the following

getting cookie value-->
textbox1.text = request.cookies("myUserID").value

Hope this solves your problem
Mark
 
I just needed to change response to request, and everything ran smoothly. I guess that everytime I was calling response.cookies("myUserID") it was overwriting the existing cookie with a blank one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top