Click here to Skip to main content
13,556,624 members
Rate this:
Please Sign up or sign in to vote.

Have a good day all of you.

Facing trouble with session timeout.

I have created a web application in 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 shows an 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 5:04am
richcb 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.
richcb 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
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:[^]
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:
   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!
SAKryukov 10-Jan-13 13:39pm
The bug with double Now is amazing. A 5.
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:
SAKryukov 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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180515.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2018
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