Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
facing problem with session in asp.net.
 
I have a first page as login to this web appl. after logged in i took label to display username who logged in.
 
i wrote this code in master page cs file....bcoz to show label in every page.
after login if the application is idle for sometime...then whenever i clicks menu link. This is the error it shows.
protected void Page_Load(object sender, EventArgs e)
    {
         lblUser.Text = Session["UserName"].ToString();
}
can anybody help me. thank you.
Posted 2-Feb-13 23:07pm
Edited 2-Feb-13 23:11pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Change your code to handle the error case as:
protected void Page_Load(object sender, EventArgs e)
{
   if(Session["UserName"] != null)
     lblUser.Text = Session["UserName"].ToString();
   else
     Response.Redirect("SessionTimeoutMessagePage.aspx");
}
  Permalink  
Comments
Kishor Deshpande at 3-Feb-13 8:21am
   
My 5
Sandeep Mewara at 4-Feb-13 2:57am
   
Thanks.
Madhuri Patil at 4-Feb-13 0:54am
   
from me 5 points
Sandeep Mewara at 4-Feb-13 2:57am
   
Thanks.
Nandakishorerao at 4-Feb-13 1:22am
   
Good answer..My 5
Sandeep Mewara at 4-Feb-13 2:56am
   
Thanks.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Quote - HYD says:
... if the application is idle for sometime ...
Your problem is probably that the session timed out.
In this case you should revive your session parameters and in any case you'd better use a check for the session parameter if it exists, or if the session exists.
 
Session["mySessParam"] == null
If the above is True, it indicates that the session has ended (or that somewhere in your code you've removed the parameter).

NOTE:
you can configure the session timeout in your web.config file to be more than the default 20 mins.
 
Cheers,
Edo
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Quote:
if the application is idle for sometime

 
As stated by Edo Tzumer your problem seems to be of session time-out.You can maintain your session time out from web.config.
 
Similar thing can be done more easily by configuring the session state settings on your IIS where you can simply put in the session time out time or select the more appropriate session mode for your application.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

There is no problem with your code just a small modification makes your program works correctly.
As mentioned above
protected void Page_Load(object sender, EventArgs e)
{
   if(Session["UserName"] != null)
     lblUser.Text = Session["UserName"].ToString();
   else
     Response.Redirect("SessionTimeoutMessagePage.aspx");
}
 
you have to change your code like this. Because when ever you clicked on some link on some aspx page in your website which associated with the masterpage it throws you an error.
Because every time the control loads your master page first and when executing the page_load event you are blindly looking for some session value.
Remember the ASP.NET doesn't show a null/empty value if the session was not created. It throws you an error for that reason.
 
So you have to check whether is there any session created or not before assigning the session value to any label to display eg: welcome XXXX
 
So kindly refer the above code now you can understand clearly why you are getting the error and not to get in your future.
 
I hope this will be useful to you. If it helps you kindly give 5 star rating and make it solved.
Thank you
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 4 Feb 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