How to use session state in ASP.NET using C#

Software developer can interact with session state using the  System.Web.SessionState.HttpSessionState class, which is provided in an ASP.NET web page as the built-in Session object. Items can be added to the collection and retrieved after in the same manner as items can be added to a page’s view state:

For example, software developer might store a string in memory like this:

Session[“FirstName”] = firstName;

Then he can retrieve it with an appropriate conversion operation:

firstName = (string)Session[“FirstName”];

Session state is global to entire web application for the current user. The session state can be lost if:

–          The user closes and restart the browser
–          The user accesses the same page through a different browser window. Browsers differ on how handle situation like this.
–          The session times out due to inactivity.
–          The developer ends the session in code.

In the first two cases, the session remains in memory, because the web server “does not know” that the client has closed the browser or change windows. The session will stay in memory, remaining inaccessible, until it expires.