Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting and getting cookie

Status
Not open for further replies.

KnotGoblin

Technical User
Jan 4, 2005
77
US
I am having trouble retreiving a cookie (or setting for that matter).

I am using the following to retreive the cookie on page_load
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Request.Cookies("pref1") Is Nothing Then
        Trace.Write("cookie value = " & Request.Cookies("pref1").Value)
    Else
        Trace.Write("cookie doesn't exist")
    End If
End Sub

here is the code that sets the cookie.
Code:
Private Sub savePref(ByVal pref1 as Integer)
    Dim myCookie As New HttpCookie("pref1")
    myCookie.Value = pref1
    myCookie.Expires = DateTime.MaxValue
    Response.Cookies.Add(myCookie)
End Sub

If i view the cookie in firefox, the cookies has no attributes.
And for IE the cookie never shows up.

-Jer
 
Seems you are setting it incorrecly. You set the value with an integer but then try to get it with the name "pref1"

 
Seems you are setting it incorrecly. You set the value with an integer but then try to get it with the name "pref1"
yeah. And the problem is....?

-Jer
 
YOu create the name as say "1" with the creation of the cookie then try to get the "pref1" cookie. They are 2 different cookies
 
i don't know if i agree...when the cookie is declared, it's given the name pref1, the value of the cookie is the integer value of pref1, name should still be pref1, so that may not be the issue...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
i am naming the cookie "pref1". (notice the quotes. its a string.)
It just so happens that the variable i am using to assign the the cookie's value is also pref1.

-Jer
 
The cookies shows up when i do a trace.

but when i try to put the cookie value in a textbox:
Code:
Me.txtPref1.Text = Request.Cookies("pref1").Value

i get:
Code:
 [b]Exception Details:[/b] System.NullReferenceException: Object reference not set to an instance of an object.
which i guess is indicating a null value for the cookie.

-Jer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top