Session state is one of the most important pieces of state in any application because it is associated with the particular client making the request. This is most important for applications where you want to have the state associated with a single user available (as in the case of a shopping cart).
Session state is always available through the Page
(and through the HttpContext
) via the Session
object. It's a name value dictionary that holds any kind of CLR object. Adding and retrieving information is accomplished most easily via indexers. In addition, session state may be configured in its location, in how it's tracked, and in how long it lasts. ASP.NET supports a number of other more advanced settings, too.
In this tutorial, we also looked at the Wizard
control as a way to retain information between several posts without resorting to session state. This is most useful when several kinds of related data need to be collected at once.
Tutorial 13 Quick Reference
How to access the current client's session state
Use the
Page.Session
propertyUse the current context's
HttpContext.Session
property
How to access a specific value in the current client's session state
Session state is a set of key-value pairs. Access the data with the string key originally used to insert the data in the cache
How to store session state in-proc
Set the <SessionState> attributes in Web.Config. Set mode to InProc
How to store session state in a state server
Set the <SessionState> attributes in Web.Config. Set mode to StateServer. Be sure to include a stateConnection string
How to store session state in SQL Server
Set the <SessionState> attributes in Web.Config. Set mode to SQLServer. Be sure to include a sqlConnection string
How to disable session state
Set the <SessionState> attributes in Web.Config. Set mode to Off
How to use cookies to track session state
Set the <SessionState> attributes in Web.Config. Set cookieless to false
How to use URL to track session state
Set the <SessionState> attributes in Web.Config. Set cookieless to true
How to set session state timeout
Set the <SessionState> attributes in Web.Config. Set timeout to a value (representing minutes)