Board logo

subject: Using Session State In Asp.net [print this page]


ASP.NET is used for developing great websites using the .net framework. While making websites, it is necessary to store session specific information such as name when user logs into account and displays name on the page. This helps in making website user interactive and customization based on user preference. Session state is an example of server side state management, which means that information is available at server side only, it can also be used to track the number of users logged in a website, number of visitors visited in day, month or year, traffic based on countries, number of clicks by a user and so on. This also helps in applications such as ads where the money has to be paid on the basis of number of clicks by a user.

The scope of session state is limited to the current browser session. If multiple users are accessing the website, each of them will have different session state. If a user exits from a website and logs back on, the user will have different session state. Session state is structured as a key-value pair for storing session specific information that needs to be maintained between server round trips and between requests for pages.

When a user first requests a page from an Asp.net website, website automatically adds a session cookie to the client browser. Each session is identified by a unique 20-bit session id string containing ASCII characters. Session state contains timeout attribute that means if the session is inactive for particular time period, it abandons the session. The default timeout is 20 minutes.

Storing and retrieving of a session:

We can store the session by using a variable

Session["name"]="Mitesh";

We can retrieve it from another page using

String value = (string) Session["name"];

Note that, we have to typecast because the session is stored as an object and it has to be retrieved in from of string.

Session state is global to the application for the current user. However, it can be lost in one of the following ways:

When Session.Abandon() method is called within web page code

When user closes and restarts the browser

When the user accesses the same web page through a different browser window

When the session times out because of inactivity

by: NeerajSwt




welcome to loan (http://www.yloan.com/) Powered by Discuz! 5.5.0