How can I check if a cookie value has been set in C#?

Posted on 2006-06-02
Medium Priority
Last Modified: 2008-02-20
I have a situation where I won't know whether or not a cookie value has been set, and I need a way to check this.

In my code behind (test.aspx.cs) I have the following line

   Label.Text = Request.Cookies["somecookie"].Value;

This will throw a NullReferenceException if "somecookie" was never set, and from that point on, I can't read any other cookie info.

How can I test whether or not this cookie was set before applying the ".Value" to the cookie object?
Question by:cgipisani
1 Comment
LVL 18

Accepted Solution

Elvio Lujan earned 2000 total points
ID: 16817645
if (Request.Cookies["somecookie"] != null)
    Label.Text = Request.Cookies["somecookie"].Value;

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question