Session Timeout in asp.net
In previous post we learnt how to use session in asp.net. In this post we will learn some more things about session.
Session is a length/period of time that a particular browser instances spends accessing a website. The Session object is used to persist data across a user session during the user’s visit to a website.
In asp.net by default session timeout = 20 minutes, but in some cases we need to change session time increment or decrement by changing web.config file setting.
We can also set manually by write c# code at code behind .aspx page in asp.net.
Set Session Timeout
- Session Timeout using web.config file
- Session timeout in each page using C# code
- Session timeout using IIS Server for website
Using Web.config file
Set session timeout = 3 minute using web.config file.
<system.web>
<sessionState mode=”InProc” timeout=”3″></sessionState>
</system.web>
Using C# code
We can also set session timeout for each page.
Session timeout syntax
Session.Timeout= 2;
Above line indicate that all session will be timed out after 2 minute if user doesn’t visit any new page of site.
Example of Session.Timeout
If the user doesn’t do anything on web for for a long time then they automatically logged out from web site, this things done by the Timeout property of Session in ASP.Net.
Let’s understand more about session timeout with asp.net example. Open visual studio and add new web form in website. Design web form with two button control, a label control and a textbox control as show in below figure.
Here, the button set timeout will be set manually session timeout = 2 minute, after clicking set timeout button user doesn’t visit any new pages the session will be expired and user get “session null exception” error message.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsessiontimeout_Click(object sender, EventArgs e)
{
//create session and set timeout = 2 minutes
Session["name"] = txtname.Text;
Session.Timeout = 2;
}
protected void btndisplay_Click(object sender, EventArgs e)
{
lbl.Text = Session["name"].ToString();
}
Now, If our web form stays idle more then 2 minute continuously, then after you will try to get session value, you will have below error message.
After session timeout, we got below error message.
Set Session Timeout using IIS Server
Open IIS setting
Open run dialog box — > type inetmgr and press enter –> IIS Manage
Open Control Panel –> Administrative Tools –> IIS Manager
Select Default website –> right click and select properties
Select ASP.NET section , and click on Edit Configuration
Select “State Management” option
This windows Time-out(in minutes) textbox write new value to change session timeout.
Related ASP.Net Topics :
Session State in C#.Net
View State in C#.Net
Subscribe us
If you liked this asp.net post, then please subscribe to our YouTube Channel for more asp.net video tutorials.
We hope that this asp.net tutorial helped you to understand about example of Session Timeout in C#.
Next asp.net tutorial we will understand more about session with Session Example.