Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 10-Jan-13 6:04am
Comments
richcb at 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 at 10-Jan-13 11:17am
   
No Sir, Please help me how to add this.
richcb at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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!
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 10-Jan-13 13:39pm
   
The bug with double Now is amazing. A 5.
—SA
OriginalGriff at 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 at 10-Jan-13 14:12pm
   
:-)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100