32

I want to check that session is null or empty i.e. some thing like this:

if(Session["emp_num"] != null)
{

   if (!string.IsNullOrEmpty(Session["emp_num"].ToString()))
            {
                //The code
            }
}

Or just

 if(Session["emp_num"] != null)
    {

       // The code
    }

because sometimes when i check only with:

       if (!string.IsNullOrEmpty(Session["emp_num"].ToString()))
                {
                    //The code
                }

I face the following exception:

Null Reference exception

Ajay2707
  • 5,624
  • 6
  • 38
  • 56
Anyname Donotcare
  • 10,649
  • 59
  • 212
  • 375
  • 2
    have a look at this post http://stackoverflow.com/questions/234973/what-is-the-best-way-to-determine-a-session-variable-is-null-or-empty-in-c – Bobby Aug 24 '11 at 09:04

6 Answers6

66

Use this if the session variable emp_num will store a string:

 if (!string.IsNullOrEmpty(Session["emp_num"] as string))
 {
                //The code
 }

If it doesn't store a string, but some other type, you should just check for null before accessing the value, as in your second example.

Roy Goode
  • 2,900
  • 19
  • 22
10
if (HttpContext.Current.Session["emp_num"] != null)
{
     // code if session is not null
}
  • if at all above fails.
Nirali
  • 101
  • 1
  • 2
6

You need to check that Session["emp_num"] is not null before trying to convert it to a string otherwise you will get a null reference exception.

I'd go with your first example - but you could make it slightly more "elegant".

There are a couple of ways, but the ones that springs to mind are:

if (Session["emp_num"] is string)
{
}

or

if (!string.IsNullOrEmpty(Session["emp_num"] as string))
{
}

This will return null if the variable doesn't exist or isn't a string.

ChrisF
  • 131,190
  • 30
  • 250
  • 321
  • In newer versions of C# you can give a name to the variable when using is: `if (Session["emp_num"] is string myString) { // myString will not be null }` – Peheje Feb 18 '20 at 11:23
2

If It is simple Session you can apply NULL Check directly Session["emp_num"] != null

But if it's a session of a list Item then You need to apply any one of the following option

Option 1:

if (((List<int>)(Session["emp_num"])) != null && (List<int>)Session["emp_num"])).Count > 0)
 {
 //Your Logic here
 }

Option 2:

List<int> val= Session["emp_num"] as List<int>;  //Get the value from Session.

if (val.FirstOrDefault() != null)
 {
 //Your Logic here
 }
Chandan Kumar
  • 219
  • 10
  • 14
2

You should first check if Session["emp_num"] exists in the session.

You can ask the session object if its indexer has the emp_num value or use string.IsNullOrEmpty(Session["emp_num"])

slugster
  • 48,492
  • 14
  • 96
  • 143
Peter
  • 27,049
  • 8
  • 63
  • 82
1

Check if the session is empty or not in C# MVC Version Lower than 5.

if (!string.IsNullOrEmpty(Session["emp_num"] as string))
{
    //cast it and use it
    //business logic
}

Check if the session is empty or not in C# MVC Version Above 5.

if(Session["emp_num"] != null)
{
    //cast it and use it
    //business logic
}
vishpatel73
  • 267
  • 3
  • 9