Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Wednesday, August 8, 2012

Session state in Asp.Net

In this article we will discuss about session state state management technique. You can also check View State, Query String and Cookies.

Through cookies or query string it is very difficult to store and access complex information like custom data objects. Session allows you to store any type of data in memory on the server. The information is protected, because it is never transmitted to the client. Every client that accesses the application has a different session.
Most of the ecommerce site uses session mechanism in

ASP.NET tracks each session using a unique 120-bit identifier. That is known as sessionid which is transmitted between the web server and the client.

The session id is submitted through a cookie name as ASP.NET_SessionId. Or a Session ID is transmitted in a specially modified URL. This is used in case where the client does not support cookies.
- Session state is global to your entire application for the current user.

- It stores in the server memory, so it might be create a performance issue.

But there are certain situation where Session could be lost:

- If the user closes and restarts the browser.

- If the user accesses the same page through a different browser window, although the session will still exist if a web page is accessed through the original browser window. Browsers differ on how they handle this situation.

- If session time out occurs.

- If you call the method Session.Abandon() programmatically in any web page of the site.

- If restart IIS.

Below is an example that stores a dataset in the session:


You can also check some Ado.Net articles here.
To retrieve the value we can write like below:

DataSet ds = new DataSet();
ds = (DataSet) ession["Employees"];

Session state can be configured in web.config file inside <system.web> tag like below:
<sessionState timeout="20" mode="InProc" />

There are also some othe parameters available.

- Timeout: This specifies the number of minutes that ASP.NET will wait, without receiving a request, before it abandons the session. This represents in minutes and default is 20 min.

Through code you can give like below:
Session.Timeout = 30;

- Mode:
There are 3 different modes available in Asp.Net like:
This is the default session state mode and mostly used in small web sites.
Inproc mode will not work in web farm environment.
Here in this mode Asp.Net uses a different windows service for state management. We need to start the Asp.Net state service.

Open Services.msc, it will open the services then search for Asp.Net state service and start.

SQL Server:
In this mode Asp.Net stores the session in SQL Server. You can read this article on How to Implement SQLServer authentication in

We can also off the session state by using mode attribute like

<sessionState mode="Off" />

This will improve some performance of the web site.

0 on: "Session state in Asp.Net"