1. aspnet-session

Session - (ASP.NET Web Forms)

In ASP.NET Web Forms, the Session object is used to store and retrieve user-specific data across multiple requests. This page provides an overview of Session, including its syntax, example, and usage.


The Session object is accessed via the HttpContext.Current.Session property. Here is the syntax for accessing and using Session:

// To set a session variable:
Session["variableName"] = variableValue;

// To get a session variable:
var variableValue = Session["variableName"];

// To remove a session variable:

// To clear all session variables:


Here's an example of how to use Session to store and retrieve user-specific data:

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        // Set the session variable
        Session["UserName"] = "John Doe";

protected void Button_Click(object sender, EventArgs e)
    // Retrieve the session variable
    string userName = Session["UserName"].ToString();

    // Display the variable value
    Label.Text = "Welcome " + userName;


When the user visits the page for the first time, the Page_Load method is called, and the "UserName" session variable is set to "John Doe". When the user clicks the button, the Button_Click method is called, and the "UserName" session variable is retrieved and displayed in a label.


Session is an important concept in ASP.NET. It allows you to store information between HTTP requests. When a user visits a page for the first time, a new session is started, and a unique session ID is generated. This ID is stored in a cookie on the user's machine (or passed as a URL parameter) and sent back to the server on subsequent requests.

Session is useful when you need to store user-specific data (such as shopping cart items, user preferences, or authentication tokens) across multiple requests. You can store any object in Session, and it will be available for the duration of the user's session (which typically lasts for 20 minutes by default, but can be configured).


Session is commonly used in ASP.NET Web Forms applications to store and retrieve user-specific data. Some common scenarios where Session can be useful include:

  • Storing user preferences (such as font sizes, color schemes, or language preferences)
  • Storing authentication tokens or user session information
  • Storing data that needs to be shared across multiple pages (such as shopping cart items or search results)

Important Points

  • Session allows you to store and retrieve user-specific data across multiple HTTP requests
  • Session can store any object and is available for the duration of the user's session
  • The session timeout can be configured in the web.config file
  • Session data is stored on the server, so it can potentially consume a large amount of server memory if not used carefully
  • You should avoid storing sensitive data (such as passwords or credit card numbers) in Session


In this page, we discussed the Session object in ASP.NET Web Forms, including its syntax, example, output, explanation, use, and important points. Session is an important tool for storing and retrieving user-specific data in an ASP.NET Web Forms application. By using Session, you can make your application more user-friendly and customizable.

Published on: