Export (0) Print
Expand All

Code: Saving Values in Session State (Visual C#)

Visual Studio .NET 2003

This example uses the Session.Add method to persist values within an individual session.

Example

string firstName = "Jeff";
string lastName = "Smith";
string city = "Seattle";

Session.Add("First", firstName);
Session.Add("Last", lastName);
Session.Add("City", city);

Compiling the Code

Start a new ASP.NET application and paste the code into the Page_Load method.

Robust Programming

Storing information in session state uses server memory. An alternative is to store information in view state. For more information, see Code: Saving a Value in View State (Visual C#) and State Management Recommendations.

Server state can expire after 20 minutes of inactivity (by default) and the information that you store there can be lost.

See Also

HttpSessionState Class | Introduction to Web Forms State Management | Saving Web Forms Page Values Using View State | State Management Recommendations | ASP.NET State Management | Web Applications Example Topics

Show:
© 2014 Microsoft