Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello FRIENDS,

Have a good day all of you.

Facing trouble with session timeout.
----------------------------------

I have created a web application in Asp.net c#, Sql Server 2005.

but am facing a problem with session timeout.

my first page is login.aspx, this page is used to access/open the application.

after user login, if his/her username and password is correct. it will redirect to the main pages, here he will see/access all the webpages.

Suppose, if i stay idle for some time on this application....it shows an error.

THIS IS THE ERROR...

=================================================================

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 18: lblDate.Text = System.DateTime.Now.ToLongDateString();
Line 19: lblTime.Text = System.DateTime.Now.ToShortTimeString();
Line 20: lblWelcomeMSG.Text = Session["UserName"].ToString();
Line 21: }
Line 22: }

====================================================================

Please help me and SUGGEST me, how to solve me this problem.

Thanks in Advance.
Posted
Comments
Richard C Bishop 10-Jan-13 11:12am    
Do you have the timeout attribute set to anything in the session tag of the web.config file?
Software Engineer 892 10-Jan-13 11:17am    
No Sir, Please help me how to add this.
Richard C Bishop 10-Jan-13 11:51am    
Here is the code to add to your web.config file. It belongs between the SEYSTEM.WEB tags. SESSIONSTATE timeout="20". However, the solution below is probably a better way to handle this.

1 solution

You can increase the timeout, but you shouldn't have to.
To increase it, see here: http://msdn.microsoft.com/en-us/library/h6bb9cz9(vs.80).aspx[^]
But you should cope with the session timing out gracefully, by checking, or by using a try...catch block.
Oh, and by the way, never read the date and time twice - read it once, store it in a variable, and use it twice instead:
C#
try
   {
   DateTime now = System.DateTime.Now;
   lblDate.Text = now.ToLongDateString();
   lblTime.Text = now.ToShortTimeString();
   lblWelcomeMSG.Text = Session["UserName"].ToString();
   ...
   }
catch (Exception ex)
   {
   // Log it, or report to the user that he needs to log in again.
   }
If you read twice, you can get different days, months, or even years and this can cause subtle bugs which are hard to track down. In this case, it just presents a bad date and time combo to the user, but if you use similar code for databases you can truly mess up your data!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Jan-13 13:39pm    
The bug with double Now is amazing. A 5.
—SA
OriginalGriff 10-Jan-13 13:51pm    
It's certainly fun to find the first time you get it reported! Especially as if generally isn't noticed for a few months until someone notices the data is wrong. :laugh:
Sergey Alexandrovich Kryukov 10-Jan-13 14:12pm    
:-)

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