cookie value dissappears

Codeless

Well-known member
Joined
Jan 24, 2003
Messages
59
Location
Great NW
On the load of a certain .aspx web page I check to see if a cookie exists with an email address inside (in other words they have used this page before and an email address was entered). If it exists, it is automatically put into a textbox on the form. However this only works once and as soon as the textbox is populated the email address is gone from the cookie. Almost like it was cut out of the cookie and not copied so the next time I use the cookie the email address isnt there anymore. Here is the code:

Code:
Private Sub Page_Load(...)
If Request.Browser.Cookies then
   If Request.Cookies("email") is Nothing then
      Dim cookEmail As New HttpCookie("email", "")
      cookEmail.Expires = DateTime.Now.AddDays(14)
      Response.Cookies.Add(cookEmail)
   Else
      Dim cookEmail as HttpCookie = Request.Cookies("email")
      If Me.TextBox1.Text = "" Then Me.TextBox1.text = cookEmail.Value
      Response.Cookies("email").Expires = DateTime.Now.AddDays(14)
   Endif
Endif
End Sub

If I step through this code it always goes through the else branch and when its completed and the web page appears I look into the cookie and the value is gone. Thanks for at least looking. Anyone?
 
What if you change your

"Response.Cookies.Add(cookEmail)"

to

"Response.AppendCookie(cookEmail)"

Any different?
 
didnt seem to work. in fact it never really goes into the if branch of that code because it detects a proper cookie. The problem ocurrs when it reads the original cookie for the first time. The email is there, and is placed in the correct location on my form. However the email is no longer in the file after that and never comes in again. Thanks for your time.
 
Try to chage line:

Response.Cookies("email").Expires = DateTime.Now.AddDays(14)

to:

cookEmail.Expires = DateTime.Now.AddDays(14)

It seems that the first line creates "second" cookie with no value. Ive checked trace info.
 
Back
Top