Introduction:

  • In this article we will learn how to use Session in asp.net or what is Session in asp.net or why Session in asp.net 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

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

Let’s see a small example of Session:

Session.aspx:

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

 

Session.aspx.cs:

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) *                         
                    Convert.ToDecimal(Session["rate"]);
            lbltotalamount.Text = Totalamount.ToString();
        }
    }
}

 

Output:

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.

comments


Leave a comment

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

You may also like