|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Read Values from Session State
This example accesses theproperty to retrieve the values in session state.
Compiling the Code
This example requires:
A Web Forms page or class that has access to the current request context using theproperty in an ASP.NET application that has session state enabled.
No exception is thrown if you attempt to get a value out of session state that does not exist. To be sure that the value you want is in session state, check first for the existence of the object with a test such as the following:
If you attempt to use a nonexistent session state entry in some other way (for example, to examine its type), aexception is thrown.
Session values are of type. In Visual Basic, if you set Option Strict On, you must cast from type Object to the appropriate type when getting values out of session state, as shown in the example. In C#, you should always cast to the appropriate type when reading session values.