• In this article we will learn how to use Session in or what is Session in or why Session in with example.
  • Description:

Asp.Net Session is a State Management Technique. A Session can store the value on the Server like string, integer, Boolean, object etc. It can support any type of object to be stored along with our own custom objects. A session is one of the best concepts for State Management because it stores the data for every user separately and the data is secured also because it is on the server.

ASP.Net maintains session state by providing a unique session ID to the client when the session creates. This session ID is stored in an HTTP cookie that the client sends to the server on each request. The server can then read the session ID from the cookie and retrieves the serialized data from the state server and type casts the object. By default Sessions will use cookies to store the data.

Session configuration setting in config.web

    <sessionState mode="InProc" customProvider="DefaultSessionProvider" timeout="20"   
 cookieless="false" >
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=, Culture=neutral,
  PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />

Let’s see a small example of Session:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Session.aspx.cs"
Inherits="AspDotNet.Session" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <td> Product Name:</td>
                <asp:TextBox ID="txtproductname" runat="server"></asp:TextBox></td>
                <asp:TextBox ID="txtqunatity" runat="server"></asp:TextBox></td>
            <td colspan="2">
                <asp:Button ID="btncalculate" runat="server" Text="Calculate" OnClick="btncalculate_Click" /></td>           
            <td> Product Name: </td>
            <td><asp:Label ID="lblproductname" runat="server"
          Text="Label" style="font-weight: 700"></asp:Label></td>
            <td><asp:Label ID="lblquantity" runat="server" Text="Label" style="font-weight: 700"></asp:Label></td>
            <td>Total Amount: </td>
            <td><asp:Label ID="lbltotalamount" runat="server"
            Text="Label" style="font-weight: 700"></asp:Label> </td>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
    public partial class Session : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                if (Session["rate"] == null)
                    Session["rate"] = 200;
        protected void btncalculate_Click(object sender, EventArgs e)
            lblproductname.Text =txtproductname.Text;
            lblquantity.Text =txtqunatity.Text;
            var Totalamount = Convert.ToDecimal(lblquantity.Text) *                         
            lbltotalamount.Text = Totalamount.ToString();



How to use Session in Asp.Net

We can remove session using one of the following 4 methods:

  1. Session.Remove(strSessionName):- Removes an item from the session state collection
  2. Session.RemoveAll():- Removes all items from the session collection.
  3. Session.Clear():- it is same as sesion.RemoveAll() method.
  4. Session.Abandon():-Cancels the current session.


I hope this article will help to you.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like