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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookie reading/writing problem

Status
Not open for further replies.

jkl

Programmer
May 16, 2001
83
US
I'm getting the following error:
[NullReferenceException: Object reference not set to an instance of an object.]


It's referencing line 70 (indicated with ** below) in my codebehind:

Dim newCookie As New HttpCookie("userInfo")
newCookie.Expires = "1/1/2002"
newCookie.Values.Add("author", "me")
newCookie.Values.Add("message", "the quick brown fox jumped over the lazy dog")

Dim userCookie As HttpCookie
userCookie = Request.Cookies("userInfo")

** Response.Write(userCookie.Expires.ToString() & &quot;<br>&quot;)

What am I doing wrong?
 
Welcome to OOP! This is something I have had to get used to also.
Anytime you get that error it means that you haven't actually made an instance of the object. You have defined what type it is but have not made it yet.

To declare an instance of the object type you need one of these

dim object as objectType
set object = new objectType

or
dim object as new objectType

or in your case

Dim userCookie As new HttpCookie

The second way does two things at once, it tells the new variable what type it is and sets it to an new instance of that type.

Hope this will help you in the future. That'l do donkey, that'l do
[bravo] Mark
 
ah ha!!

figured out the problem. it wasn't in the code to read the cookie. the problem was the cookie was not being written in the first place, so it did not exist.

i forgot to add this line to the cookie population stuff.

Response.Cookies.Add(newCookie)

Now I have another problem. I can't seem to control the expiration date of the cookie.

if i use newCookie.Expires = now(), this works fine.
but i can't set the date to anything beyond today's date.

code:
If newCookie.Expires < Now() Then
Dim newDate As Date
newDate = DateAdd(DateInterval.Day, 30, Now())
newCookie.Expires = newDate
End If

When I run this process, it sets the expiry date, once to 30 days ahead. But on reload, the expiry gets changed to 1/1/0001 12:00:00 AM.

Any ideas?
 
now() + 365

to make it last a year, and so on. :)
penny1.gif
penny1.gif
 
grrr.

still having issues with the expiry date.

it'll set once when the pager loads, but if i refresh, the date goes to 1/1/0001 12:00:00 AM

here are my functions:

Public Sub createUserCookie()
If Request.Cookies(&quot;userInfo&quot;) Is Nothing Then
Dim newDate As DateTime = DateAdd(DateInterval.Day, 365, Now())
userCookie = New HttpCookie(&quot;userInfo&quot;)
userCookie.Expires = New DateTime(Year(newDate), Month(newDate), Day(newDate))
userCookie.Item(&quot;site&quot;) = &quot; Response.AppendCookie(userCookie)
Else
userCookie = Request.Cookies(&quot;userInfo&quot;)
End If
End Sub

Public Sub writeUserCookie(ByVal keyName As String, ByVal keyValue As String)
Dim newCookie As HttpCookie
newCookie = Request.Cookies(&quot;userInfo&quot;)
newCookie.Item(keyName) = keyValue
Response.AppendCookie(newCookie)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top