Click here to Skip to main content
15,947,958 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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.
C#
protected void Page_Load(object sender, EventArgs e)
    {
         lblUser.Text = Session["UserName"].ToString();
}

can anybody help me. thank you.
Posted
Updated 2-Feb-13 22:11pm
v3

Change your code to handle the error case as:
C#
protected void Page_Load(object sender, EventArgs e)
{
   if(Session["UserName"] != null)
     lblUser.Text = Session["UserName"].ToString();
   else
     Response.Redirect("SessionTimeoutMessagePage.aspx");
}
 
Share this answer
 
Comments
Kishor Deshpande 3-Feb-13 8:21am    
My 5
Sandeep Mewara 4-Feb-13 2:57am    
Thanks.
madhuri@mumbai 4-Feb-13 0:54am    
from me 5 points
Sandeep Mewara 4-Feb-13 2:57am    
Thanks.
Nandakishore G N 4-Feb-13 1:22am    
Good answer..My 5
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
 
Share this answer
 
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.
 
Share this answer
 
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
 
Share this answer
 
 
Share this answer
 

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