Click here to Skip to main content
15,889,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If for 20 minutes the website is idle and later if a navigation button is clicked
an exception is thrown for the user stating that 'Object reference not set to an instance of an object'. How to avoid this?
Thanks.
Posted
Comments
DamithSL 5-May-14 6:22am    
update the question with how you check the session values

Your code have to check that if timeout, redirect the user to some default page, check this out:
redirect-to-login-page-after-session-timeout[^]
 
Share this answer
 
As a best practice you should check session exist first
C#
if(Session["myVal"] != null)  
{  
     string myVal = Session["myVal"].ToString();
}
else
{
     Response.Redirect("Login.aspx")
}

if you directly call Session["myVal"].ToString(), you will get "'Object reference not set to an instance of an object'." in case of session value null.

you may need to do this on each and every page. but there is a easy way by using global aspx file
Check these answers[^]
if you need to increase session timeout, then check these answers[^]
 
Share this answer
 
v4
Comments
S.Rajendran from Coimbatore 9-May-14 10:03am    
Earlier I didnt mention that I have master page. Anyhow you gave me a good clue and that I applied in my master page. The exception problem is solved. Thanks.
if (Session["FirstSessionTag"] != null)
{
string myVal = Session["FirstSessionTag"].ToString();
}
else
{
Session["FirstSessionTag"] = "*";
Response.Redirect("Default.aspx");
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900