Link to home
Start Free TrialLog in
Avatar of chrisryhal
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(aCookie)

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?
Avatar of tusharashah
tusharashah

On Page_Load event of your 1st Page write down some code like following:

  Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKIE")
  If aCookie Is Nothing Then
    Response.Redirect("AnotherPage.aspx")  
  End If

-tushar
Or you can simply write:

if Request.Cookies("MAPPCOOKIE")  IS NOTHING THEN
  Response.Redirect("AnotherPage.aspx")  
End if

:)

-Nauman.
Avatar of chrisryhal

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("MAPPCOOKIE") Is Nothing Then
            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\Local Settings\Temp\Cookies folder and check the result.

HTH, Nauman.
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
*
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("MAPPCOOKIE")
        aCookie.Expires = DateTime.Today.AddYears(-1)
        If Request.Cookies("MAPPCOOKIE") 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)

ok, sorry, back on this again.  I had some internal issues.  Will let you know.

Thanks,
CR
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("MAPPCOOKIE")
        aCookie.Expires = DateTime.Today.AddYears(1)
        If Request.Cookies("MAPPCOOKIE") 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.NullReferenceException: 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(Object sender, EventArgs e) in C:\Documents and Settings\Administrator\VSWebCache\www.mappinc.com\Website\Modules\Login.aspx.vb:33
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +750

 
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("MAPPCOOKIE") Is Nothing Then
          Dim aCookie As HttpCookie = Request.Cookies("MAPPCOOKIE")
          aCookie.Expires = DateTime.Today.AddYears(1)
        End If
        If Request.Cookies("MAPPCOOKIE") Is Nothing Then
            Response.Redirect("http://www.webcrawler.com")
        Else
            Response.Redirect("http://www.yahoo.com.com")
        End If
    End Sub

-tushar
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
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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