check session key is exist ?

Jak
Jak
908 Points
132 Posts

Hi,

i using mvc5.

how i can check session key is set or not (exist) ?

i do this 

if(Session["MyKey"]!=Null)
{
// do something
}

but i get error : 

Object reference not set to an instance of an object.

Views: 10111
Total Answered: 1
Total Marked As Answer: 1
Posted On: 07-Sep-2014 02:20

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
4916 Points
27 Posts
         

Hi,

try this one:

if(Session["mykey"]!=null && Session["mykey"] !="")
{
 
//do my logic
}
else
{
 
//id session["mykey"] is not set redirect
 
RedirectToAction("Index");
}

Posted On: 07-Sep-2014 02:21
 Log In to Chat