Checking to see if a cookie exists

  • Thread starter Thread starter CES
  • Start date Start date
C

CES

I'm sorry for the 101 question but I can't seem to get this to work... I'm
tiring to test if a cookie has been created before.

Everything I've seen indicates that Request.Cookies works the same in .net
as it did in old asp but for some reason I'm getting
"System.NullReferenceException: Object reference not set to an instance of
an object" when ever I attempt to test for the cookie . It doesn't return an
error if the cookie exists but does if it isn't there.

<%@ Page Explicit="TRUE" Language="vb" Debug="true"%>
<%
Dim cookie1 As string
cookie1 = Request.Cookies("testCookie")
%>

I've found lots of references on reading a cookie as long as you know it
there but I can't seem to find one that will test if it exists. I'm new to
..net from old asp and I'm totally lost, Any help on this would be
appreciated.
CES
 
You can always use Request.Browser.Cookies to see if cookie support exists for that browser.

The other option is to look to see if there are any cookies passed in first by using
if (Request.Cookies.Count > 0) {
// your cookie acces
 
hi,
if Request.Cookies["name"]!-null
then only access yhe value..
error is coz
there may be no key value corresponding to the name u hv specified..

so there is no refernce to look for...
 
Back
Top