33

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,690
  • 6
  • 40
  • 58
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • 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
  • Your first check should work and is straightforward. The second check doesn't work because you're trying to convert to a string a null object, and *afterwards* check if it's null. – Jamesckel Feb 06 '23 at 21:25

6 Answers6

68

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,940
  • 20
  • 22
12
if (HttpContext.Current.Session["emp_num"] != null)
{
     // code if session is not null
}
  • if at all above fails.
Nirali
  • 121
  • 1
  • 2
  • This seems to be the most appropriate way of handling a value like that one. Right? Using the string value it suppose to contain does not look clean to me. – carloswm85 Oct 03 '22 at 17:19
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
  • 134,786
  • 31
  • 255
  • 325
  • 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
  • 221
  • 11
  • 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
  • 49,403
  • 14
  • 95
  • 145
Peter
  • 27,590
  • 8
  • 64
  • 84
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
  • 317
  • 4
  • 10