0

I'm making Li menu visible to some users only, but after session time out its getting error:

Object reference not set to an instance.

How to avoid this error if session time out.

if (Session["LoggedInUser"].ToString() == "admin" || Session["LoggedInUserLower"].ToString() == "admin")
{
     liBatch.Visible = true;
}
else
{
     liBatch.Visible = false;
}
Salah Akbari
  • 36,933
  • 10
  • 58
  • 91
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – शेखर Aug 18 '15 at 05:37

1 Answers1

2

Check if session object is not null before calling ToString on it

if((Session["LoggedInUser"] != null && Session["LoggedInUser"].ToString()== "admin") || 
    (Session["LoggedInUserLower"] != null && Session["LoggedInUserLower"].ToString() == "admin"))
   {
        liBatch.Visible = true;
   }
   else
   {
       liBatch.Visible = false;
   }
Adil
  • 139,325
  • 23
  • 196
  • 197