473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cookie Question

ERROR:

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

This if then statement only works if a cookie exists.
It fails if the cookie does not exist.
How can I turn off lines 2 to 9 if a cookie does not exist (ie: if the
cookies does not exist then create one)?

CODE:

1 If Request.Cookies("visitor") is Nothing Then
2 dim cookie as HttpCookie
3 cookie=new HttpCookie("visitor")
4 cookie.Values.Add("cid", Session("sid"))
5 cookie.Values.Add("ckeywords",Session("skeywords") )
6 cookie.Values.Add("ccampaignno",Session("scampaign no"))
7 cookie.Values.Add("cadgroup",Session("sadgroup"))
8 cookie.Expires = Now.AddDays(365)
9 response.appendcookie(cookie)
10 end if
Nov 19 '05 #1
1 1327
This is a sample code from the MCP book.

If Request.Browser.Cookies Then
' (2) If the cookie does not exist...
If Request.Cookies("LastVisit") Is Nothing Then
' (3) Create the cookie.
Dim cookLastVisit As New HttpCookie("LastVisit", _
DateTime.Now.ToString())
' (4) Set the expiration to tommorrow.
cookLastVisit.Expires = DateTime.Now.AddDays(1)
' (5) Add to cookies collection.
Response.Cookies.Add(cookLastVisit)
' Display message.
litMessage.Text = "This is your first visit."
Else
' Get the cookie.
Dim cookLastVisit As HttpCookie = Request.Cookies("LastVisit")

' Display a message showing time of last visit.
litMessage.Text = "You last visited this page: " & _
cookLastVisit.Value
' Update the cookie on the client.
Response.Cookies("LastVisit").Value = Now.ToString()
Response.Cookies("LastVisit").Expires =
DateTime.Now.AddDays(1)
End If
Else
litMessage.Text = "Your browser does not accept cookies."
End If
Hope this helps.

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ralph Freshour | last post by:
I wrote a small .php script that creates a cookie on my local PC - I also wrote one that deletes it. My question is this: when the cookie has been created and I run the delete .php script, when...
6
by: Ajay | last post by:
hi! i am printing a simple cookie, but instead of printing um=name:blah&access:admin&exp:2312390.909 its printing um="name:blah&access:admin&exp:2312390.909" why the quotes?
1
by: koen colen | last post by:
Hello group, I hope you guys can help me out, I am modifying a piece of code from Joe Norman, I did found the code here: http://www.intranet2internet.com/public/default.asp?PAGE=sscript&ID=3 ...
1
by: Display Name | last post by:
Used one of these canned scripts to set up a JS quiz but not before having used another canned PHP script for "Tell your friend about this Web page!" sort of thing. Now i've gotta integrate them;...
7
by: What-a-Tool | last post by:
How does the expire date work setting it server side with asp. I know with javascript setting it client side it will be set to the clients local time, and therefore expire when the clients local...
7
by: odonel | last post by:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: here is my code: HttpCookie cookie = Request.Cookies;
15
by: Oleg Leikin | last post by:
Hi, (newbie question) I've created some simple .NET ASP application that should store cookies at the client machine. According to the documentation cookie expiration time is set via...
6
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My...
3
by: Dan | last post by:
Hi, I am trying to refresh the cookie to make sure the timeout is reset by simply calling a blank page on my site. I am doing this because I have an external site hosted in my web that isn't...
16
by: Stevo | last post by:
I'm guessing this is a laughably obvious answer to many here, but it's not to me (and I don't have a server or any knowledge of PHP to be able to try it). It's not strictly a PHP question, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.