c# - Object reference not set to an instance of an object.how to redirect to login page if session is null -


im opening 1 page due expiration of session , showing -

object reference not set instance of object.

aspx code

if (!ispostback)             {             if (string.isnullorempty(session["loginuser"].tostring()) == false && string.isnullorempty(session["customerid"].tostring()) == false)//error                 {                 if (session["loginuser"].tostring() == "admin")                     {                     ddlusers.visible = true;                     fillusers();                     }                 else if (session["loginuser"].tostring() != "admin" && session["customerid"].tostring() == "true")                     {                     ddlusers.visible = false;                     //fillusers();                     }                 else                     {                     ddlusers.visible = false;                     }                 fillprojectlist();                 pnl_link.visible = false;                 pnl_status.visible = false;                 }             else                 {                 response.redirect("~/login.aspx");                 }              } 

how make if session null should redirect login.aspx. without throwing run time exception.

just add session["loginuser"]!=null in first if condition after

            if (!ispostback)             {             if (session["loginuser"]!=null && string.isnullorempty(session["loginuser"].tostring()) == false && string.isnullorempty(session["customerid"].tostring()) == false)//error                 {                 if (session["loginuser"].tostring() == "admin")                     {                     ddlusers.visible = true;                     fillusers();                     }                 else if (session["loginuser"].tostring() != "admin" && session["customerid"].tostring() == "true")                     {                     ddlusers.visible = false;                     //fillusers();                     }                 else                     {                     ddlusers.visible = false;                     }                 fillprojectlist();                 pnl_link.visible = false;                 pnl_status.visible = false;                 }             else                 {                 response.redirect("~/login.aspx");                 }              } 

Comments