Help on cookies ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear all,

I try to make some test on how to use basic cookie but get some trouble in
the sens that I was not able to read back previous cretaed cookies.

In page_load event I have the following code :

If Not IsPostBack Then
If Request.Browser.Cookies Then
If Not IsNothing(Request.Cookies("PS")) Then
Session("Server") = Request.Cookies("PS").Value
txtServerName.Text = Session("Server")
End If

End If
end if

Somewhere in my code I set the cookie as follow :

If Request.Browser.Cookies Then
Dim ck_SrvName As New HttpCookie("PS")
ck_SrvName.Value = txtServerName.Text
Response.Cookies.Add(ck_SrvName)
End If

When I restart my application, my cookies object do not return it s content,
what am I doing wrong in here ?

thanks for your help
regards
serge
 
I'm not sure, but a guess is that your cookie is expiring. Are you closing
your browser and restarting when you
"restart your application" ?

If so, by default cookies only exist as long as the browser is open, to
change that you must give the cookie an expiry before adding it to the
response, something like:

cookie.Expires = DateTime.MaxValue ' Never Expires

Karl
 
Yes this is what I am doing, I am closing the browser each time to test that.
I will try to set the expiration parameter

thnaks
serge
 
And added to Karl's point..
When ever you're running session / cookie based programs, don't expect
proper results in debug mode.

Compile the code and execute in separate browser, not thru the F5'ing all
the time. So, that you can check the consistency of the code you've written.
 
Thansk , it works
I have add an expiration date and time an now when I colse my browser and
restart I am able to get the value back

by the wa where doe cokkies get sotre, I try to find out if my cooki has
been created, but how are they identify? sounds crypred

regards
serge
 
Back
Top