chrisryhal
asked on
Cookie Question
I have been successful in creating a cookie:
Dim aCookie As New HttpCookie("MAPPCOOKIE")
aCookie.Values("FirstName" ) = txtFirstName.Text
aCookie.Values("LastName") = txtLastName.Text
aCookie.Expires = DateTime.Now.AddDays(3)
Response.Cookies.Add(aCook ie)
Now, I want to have a form with VB.NET/ASP.NET on load that says if this cookie is on the machine, then open the page, if not, redirect to another page.
Can I get some help on this please?
Dim aCookie As New HttpCookie("MAPPCOOKIE")
aCookie.Values("FirstName"
aCookie.Values("LastName")
aCookie.Expires = DateTime.Now.AddDays(3)
Response.Cookies.Add(aCook
Now, I want to have a form with VB.NET/ASP.NET on load that says if this cookie is on the machine, then open the page, if not, redirect to another page.
Can I get some help on this please?
Or you can simply write:
if Request.Cookies("MAPPCOOKI E") IS NOTHING THEN
Response.Redirect("Another Page.aspx" )
End if
:)
-Nauman.
if Request.Cookies("MAPPCOOKI
Response.Redirect("Another
End if
:)
-Nauman.
ASKER
the cookie is showing up as "administrator@localhost"
What i did to test, was the following, with the cookie NOT on the system, and the label is still visible:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Request.Cookies("MAPPCOOKI E") Is Nothing Then
lblTest.Visible = True
Else
lblTest.Visible = False
End If
End Sub
What i did to test, was the following, with the cookie NOT on the system, and the label is still visible:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Request.Cookies("MAPPCOOKI
lblTest.Visible = True
Else
lblTest.Visible = False
End If
End Sub
The cookie is showing up as it has not been expired yet. Remove it from Tempoary Internet Files and in case if you are using Windows XP from c:\Documents and Settings\Administrator\Loc al Settings\Temp\Cookies folder and check the result.
HTH, Nauman.
HTH, Nauman.
ASKER
I tried deleteing the cookie as you said, and the label still shows up. in the properties of the label, I set it to false. So, the IF Statement should handle that.
The cookie has a title of "Administrator@localhost" and the contents are:
MAPPCOOKIE
FirstName=f&LastName=f
localhost/
1024
2807291008
29705152
679750496
29704549
*
The cookie has a title of "Administrator@localhost" and the contents are:
MAPPCOOKIE
FirstName=f&LastName=f
localhost/
1024
2807291008
29705152
679750496
29704549
*
Lets try this first:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKI E")
aCookie.Expires = DateTime.Today.AddYears(-1 )
If Request.Cookies("MAPPCOOKI E") Is Nothing Then
lblTest.Visible = True
Else
lblTest.Visible = False
End If
End Sub
Also, next time when you set cookie, add Expiration time like following for time being:
aCookie.Expires = DateTime.Now.AddMinutes(1)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKI
aCookie.Expires = DateTime.Today.AddYears(-1
If Request.Cookies("MAPPCOOKI
lblTest.Visible = True
Else
lblTest.Visible = False
End If
End Sub
Also, next time when you set cookie, add Expiration time like following for time being:
aCookie.Expires = DateTime.Now.AddMinutes(1)
ASKER
ok, sorry, back on this again. I had some internal issues. Will let you know.
Thanks,
CR
Thanks,
CR
ASKER
Ok, I tried to make it even easier. Here is my code, and here is the error. Again, cookies are new to me. Thanks for the help.
=============CODE========= =====
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKI E")
aCookie.Expires = DateTime.Today.AddYears(1)
If Request.Cookies("MAPPCOOKI E") Is Nothing Then
Response.Redirect("http://www.webcrawler.com")
Else
Response.Redirect("http://www.yahoo.com.com")
End If
End Sub
=============ERROR======== ========
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Members.login.Page_Load(Ob ject sender, EventArgs e) in C:\Documents and Settings\Administrator\VSW ebCache\www.mappinc.com\Website\Modules\Login.aspx.vb:33
System.Web.UI.Control.OnLo ad(EventAr gs e) +67
System.Web.UI.Control.Load Recursive( ) +35
System.Web.UI.Page.Process RequestMai n() +750
=============CODE=========
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKI
aCookie.Expires = DateTime.Today.AddYears(1)
If Request.Cookies("MAPPCOOKI
Response.Redirect("http://www.webcrawler.com")
Else
Response.Redirect("http://www.yahoo.com.com")
End If
End Sub
=============ERROR========
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Members.login.Page_Load(Ob
System.Web.UI.Control.OnLo
System.Web.UI.Control.Load
System.Web.UI.Page.Process
Seems like your Cookie is Null and you are trying to set up Expire time... try
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Request.Cookies("MAPPCOOKI E") Is Nothing Then
Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKI E")
aCookie.Expires = DateTime.Today.AddYears(1)
End If
If Request.Cookies("MAPPCOOKI E") Is Nothing Then
Response.Redirect("http://www.webcrawler.com")
Else
Response.Redirect("http://www.yahoo.com.com")
End If
End Sub
-tushar
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Request.Cookies("MAPPCOOKI
Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKI
aCookie.Expires = DateTime.Today.AddYears(1)
End If
If Request.Cookies("MAPPCOOKI
Response.Redirect("http://www.webcrawler.com")
Else
Response.Redirect("http://www.yahoo.com.com")
End If
End Sub
-tushar
ASKER
Now, one more thing. How to read the value of what is in the cookie? Example, the cookie has a value of "email"
On the Load_Event, how to display that value in txtEmail.Text
Other than that, its working now
On the Load_Event, how to display that value in txtEmail.Text
Other than that, its working now
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much. I have learned so much. I am coming from working with VB.NET WinApps, and wanted to get more into WebApps. Thanks again.
CR
CR
Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKI
If aCookie Is Nothing Then
Response.Redirect("Another
End If
-tushar