troubleshooting Question

Setting and reading Cookies in VB.Net

Avatar of megnin
megninFlag for United States of America asked on
ASP.NETVisual Basic.NET
26 Comments1 Solution376 ViewsLast Modified:
I can't figure out what I'm doing wrong.  I copied the code from another of my web forms that works fine and I've tried several "Correct Answer" examples from EE using different methods.  My Cookies are always "Is Nothing"!

In my input form (three methods. two are commented out):
            ''Set cookies for values to be read when Employer logs in again - Method 1
            'Dim SYEPCookie As New HttpCookie("SYEPInfo")
            'SYEPCookie.Values("EmployerID") = txtPriKeyEmployerID.Text
            'SYEPCookie.Values("TaxID") = txtFedID.Text
            'SYEPCookie.Expires = DateTime.Now.AddMonths(12)
            'Response.Cookies.Add(SYEPCookie)
            ''Response.Cookies("SYEPInfo").Domain = "SYEP.WF1Broward.com"

            ''Set cookies for values to be read when Employer logs in again - Method 2
            'With Response
            '    .Cookies("SYEPInfo")("EmployerID") = txtPriKeyEmployerID.Text
            '    .Cookies("SYEPInfo")("TaxID") = txtFedID.Text
            '    .Cookies("SYEPInfo").Expires = DateTime.Now.AddMonths(12)
            'End With

            ''Set cookies for values to be read when Employer logs in again - Method 3
            Dim SYEPCookie As HttpCookie = New HttpCookie("SYEPInfo")
            SYEPCookie.Values.Add("EmployerID", txtPriKeyEmployerID.Text)
            SYEPCookie.Values.Add("TaxID", txtFedID.Text)
            SYEPCookie.Expires.AddMonths(12)
            Response.Cookies.Add(SYEPCookie)
            'Response.Cookies("SYEPInfo").Domain = "SYEP.WF1Broward.com"
Several tries here too...
In the Login page trying to read the Cookies.  I can't even get a Response.Write to show up.
If I leave out the "If Is Nothing" then I get "Object reference not set to an instance of an object":
        'If Not Request.Cookies("SYEPInfo") Is Nothing Then
        '    Response.Write(Request.Cookies("SYEPInfo")("EmployerID").ToString())
        '    Response.Write(Request.Cookies("SYEPInfo")("TaxID").ToString())
        'End If

        'If Not IsPostBack Then
        '    If Not Request.Cookies("SYEPInfo") Is Nothing Then
        '        Dim SYEPCookie As HttpCookie = Request.Cookies("SYEPInfo")
        '        txtEmployerID.Text = Server.HtmlEncode(Request.Cookies("SYEPInfo")("EmployerID"))
        '        txtTaxID.Text = Server.HtmlEncode(Request.Cookies("SYEPInfo")("TaxID"))
        '        'Response.Write(Server.HtmlEncode(Request.Cookies("EmployerID").Value) + "ok")
        '        Response.Write(Server.HtmlEncode(Request.Cookies("SYEPInfo")("EmployerID")))
        '        Response.Write(Server.HtmlEncode(Request.Cookies("SYEPInfo")("TaxID")))
        '    End If
        'End If

        'If Not Page.IsPostBack Then
        Dim SYEPCookie As HttpCookie = Request.Cookies("SYEPInfo")
        'If Not SYEPCookie Is Nothing Then
        Response.Write(SYEPCookie("EmployerID"))
        Response.Write(SYEPCookie("TaxID"))
        'End If
        'End If

Edit: I just noticed that in "Temporary Internet Files" where all the other Cookies live, what I find there are no cookies, but a "ScriptResource.axd?d=HcP8XCZx...=ffffffff823b7694" thing. So, my cookies don't seem to be getting cooked right.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 26 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 26 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros