Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:58:35+00:00 2026-06-10T04:58:35+00:00

I need to check if cookie is present with value or not. But I

  • 0

I need to check if cookie is present with value or not. But I wonder if there is some quick and good way of doing so since if I need to check 3 cookies it seems bad to check with if or try.

Why it does not assign empty string to my variable if cookie is not present? Instead it shows Object reference not set to an instance of an object.

My code (it works, but it seems too big for this task, I think there should be a better way of doing this)

// First I need to asign empty variables and I don't like this
string randomHash = string.Empty;
string browserHash = string.Empty;
int userID = 0;

// Second I need to add this huge block of try/catch just to get cookies
// It's fine since I need all three values in this example so if one fails all fails
try
{
    randomHash = Convert.ToString(Request.Cookies["randomHash"].Value);
    browserHash = Convert.ToString(Request.Cookies["browserHash"].Value);
    userID = Convert.ToInt32(Request.Cookies["userID"].Value);
}
catch
{
    // And of course there is nothing to catch here
}

As you can see I have this huge block just to get cookies. What I would like is something like this:

// Gives value on success, null on cookie that is not found
string randomHash = Convert.ToString(Request.Cookies["randomHash"].Value);
string browserHash = Convert.ToString(Request.Cookies["browserHash"].Value);
int userID = Convert.ToInt32(Request.Cookies["userID"].Value);

Edit
Maybe I can somehow override the .Value method to my liking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:58:36+00:00Added an answer on June 10, 2026 at 4:58 am

    Just check if the cookie is null:

    if(Request.Cookies["randomHash"] != null)
    {
       //do something
    }
    

    NOTE: The “Better” way of doing this is to write good code that is both readable and reliable. It doesn’t assign empty string because this is not how C# works, you are trying to call the Value property on a null object (HttpCookie) – you cannot use null objects because there is nothing to use.

    Converting to an int you still need to avoid parse errors, but you can use this built in method:

    int.TryParse(cookieString, out userID);
    

    which brings on another point? Why are you storing the userID in a cookie? this can be changed by the end user – I don’t know how you plan on using this but would I be right to assume this is a big security hole?


    or with a little helper function:

    public string GetCookieValueOrDefault(string cookieName)
    {
       HttpCookie cookie = Request.Cookies[cookieName];
       if(cookie == null)
       {
          return "";
       }  
       return cookie.Value;
    }
    

    then…

    string randomHash = GetCookieValueOrDefault("randomHash");
    

    Or with an Extension method:

    public static string GetValueOrDefault(this HttpCookie cookie)
    {
       if(cookie == null)
       {
          return "";
       }  
       return cookie.Value;  
    }
    

    then…

    string randomHash = Request.Cookies["randomHash"].GetValueOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check in some fast way if there is any text nodes
I need to check some settings for all users on Windows clients in the
I need to check if a link (a-tag) is the only content (not just
I need to check if some pictures (eps, tiff files) are in 300 DPI
I need to check if a cookie exists when the user lands on a
For every page requested, I need to check a cookie or create it if
So Im using the jquery cookie plugin and I need to check if a
I already have a script that check for the cookies existence, but i need
I need to check for a condition on each page I visit on the
I need to check for duplicates before saving to the database in the create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.