14.2. Understanding Sessions

Sessions within an ASP.NET application enable users to easily maintain application state. Sessions will remain with the user as he works through repeated calls to an application for a defined period.

Sessions are easily created, and it is just as easy to retrieve information from them. Use the following code to create a session for the user or calling application that can be accessed later in the application or to assign a value to an already established session.

Session["EmployeeID"] = Value1;

This will assign what was being held in the variable Value1 to the EmployeeID Session object. To retrieve this information from the session and then use it in your code, do the following:

Value2 = Session["EmployeeID"];

In ASP.NET, session timeout is similar to what it was in classic ASP — a session would timeout on the user after 20 minutes. If the user opened a page within a Web application (thereby creating a session) and then walked away for a cup of coffee, when the user came back to the page 40 minutes later, the session would not be there for them. You could get around this by going into the server and changing the time allotted to the session timeout property, but this was cumbersome and required that you stop the server and then start it again for the changes to take effect. In addition, because sessions are resource intensive, you would not want to store too many sessions for too long.

With ASP.NET it is now possible to change the session timeout property ...

Get Professional ASP.NET 3.5 AJAX now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.