Checking to see if a cookie exists

Discussion in 'ASP .Net' started by CES, Feb 12, 2004.

  1. CES

    CES Guest

    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 from old asp and I'm totally lost, Any help on this would be
    CES, Feb 12, 2004
    1. Advertisements

  2. CES

    Guest Guest

    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
    Guest, Feb 13, 2004
    1. Advertisements

  3. 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...
    .NET Follower, Feb 13, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.