How to Set Session Timeout in ASP.Net using web.config.

In this asp.net post, we will learn how to set session timeout property in asp.net using web.config file.

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 the 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.

here, we have set session timeout 1 minute in web.config.

    <system.web>
<sessionState timeout="1"></sessionState>
</system.web>

 

Here, if we use session object then the session will expire after 1 minute automatically.

let’s understand with an example how to expire session and which kind of error generate while session expire.

Here, we design two web forms and send value from one web forms to second web forms using session variable.
and we set the session expire time to 1 minute in web.config file. if our second page goes in idle position up to one minute, after 1 minute we will get session object expire error on second page.

STEP 1 : Create ASP.Net web application.

after creating new web application add new web forms in asp.net application.

Design the asp.net web for like below :

Set Session timeout in asp.net using web.config
Set Session timeout in asp.net using web.config

Here, we design a web forms for input some value and send text value to second page by clicking the send button.
the send button code is:

 protected void Button1_Click(object sender, EventArgs e)
{
Session["val"] = TextBox1.Text;
Response.Redirect("sec.aspx");
}

In above code, we first store TextBox1 value to Session[“val”] object and the redirect page to sec.aspx page by clicking send button.

Set Session timeout in asp.net using web.config
Set Session timeout in asp.net using web.config

In above forms we have one button to get the session value and one label for display the session value.

The get value button code is :

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Session["val"].ToString();
}

Now, If our web form stays idle more then one minute continuously, then after you will try to get session value, you will get below error message.

Set Session timeout in asp.net using web.config
Set Session timeout in asp.net using web.config
Set Session timeout in asp.net using web.config
Set Session timeout in asp.net using web.config

I hope this asp.net session timeout example will help you…..

Leave a Reply

Your email address will not be published. Required fields are marked *