testing for cookies' existance

C

Craig Deelsnyder

in ASP, this seems to work to see if a cookie exists:

If Request.Cookies("site")("district") Is Nothing Then

In asp.net, though, it gives me a 'System.NullReferenceException: Object
reference not set to an instance of an object.'

Why is that?

Looking at MSDN, my syntax seems OK:

http://msdn.microsoft.com/library/d...s/dv_vstechart/html/vbtchASPNETCookies101.asp

-Darrel

That's because the .Cookies collection returns null/Nothing if the cookie
with the given name is not found (in this case Request.Cookies("site") is
nothing). So when the portion after that tries to call a property to get
the value, it's a null reference exception.

Check if Request.Cookies("site") is nothing first (remove the district
portion from this line of code)...then you can check the value inside that
if statement....
 
D

darrel

That's because the .Cookies collection returns null/Nothing if the cookie
with the given name is not found (in this case Request.Cookies("site") is
nothing). So when the portion after that tries to call a property to get
the value, it's a null reference exception.

Check if Request.Cookies("site") is nothing first (remove the district
portion from this line of code)...then you can check the value inside that
if statement....

aha! OK, that makes sense. And works! THanks!

-DArrel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top