Forum Moderators: open

Message Too Old, No Replies

Cookie Problem

         

andrewsmd

9:16 pm on Sep 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is wrong with this code. The way I see it, it should say cookie value is Empty and then if you refresh the page, it should say it is cookie value. However it always says it is cookie value, even when I veiw it on two different computers. Any idea why? Thanks

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim hc As New HttpCookie("test")
If hc IsNot Nothing Then
label.Text = ("Current Cookie value is " + Request.Cookies("test").Value + "<br />")
Else
label.Text = ("Current Cookie value is EMPTY!")
hc.Value = "Cookie Value"
hc.Expires = DateTime.Now.AddMinutes(2)
Response.Cookies.Add(hc)

End If

End Sub

marcel

7:08 am on Sep 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dim hc As New HttpCookie("test")
If hc IsNot Nothing Then
hc will never be Nothing as you have just created it with 'New'

Probably not the best solution, but the following works:

Dim hc As New HttpCookie("test")
If Request.Cookies("test") IsNot Nothing Then

(I would also move the Dim hc As New HttpCookie("test") to after the 'else')

I've noticed that often a problem you are experiencing could be solved by using the Visual Studio debugger, which is a very powerful tool. (the best debugger I have ever come across in any Development Studio)
In case you are not experienced with it, here is a good Introduction to Debugging with Visual Studio [asp.net], and Debugging Tips and Tricks [msdn.microsoft.com]