Session in ASP.NET using VB.NET

In this article you will learn about Session,Session variable,Session state and how you can declare and use Session variable.
  • 13341
 
A session is defined as the period of time that a unique user intracts with a web application. In ASP.NET by default session variable are stored in the SessionStateItemCollection that is exposed through the System.Web.HttpContext.Session property. As long as the user maintains their state, you can use the session object as much as you want. ASP developers who wish to retain data for unique user session can use a feature known as session state.
The session state can be configured using the Element of the System.web configuration section. The session can also be configured using the EnabledSessionState page directive. The session state is exclusive to the session in ASP.NET. when two different user make concurrent requests to a page. Seperate sessions are granted concurrently. If two requests are made for a single sessionID, then the first request is granted exclusive access while the second request will be executed on release from the first request. If the request is  for a readonly session
mode then the both requests are granted simultaneously.

Here we are taking an example of how you can declare and use session variable.
  • Firstly create a webform and place a Label having Text Enter Value, a TextBox with name txtvalue. Then Create a Button having Text Save and another button with Text GoTo Form2. The form will display like below.

    Session1.gif
     
  • Write the below code.

        
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Session("VALUE1") = txtvalue.Text
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Server.Transfer("WEBFORM2.ASPX")
        End Sub

     
  • The default.aspx looks something like below.

    <%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
        CodeBehind="Default.aspx.vb" Inherits="session._Default" %> 

    <
    asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <
    asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <asp:Label ID="Label1" runat="server" Text="Enter Value"></asp:Label>
    &nbsp;&nbsp;
        <asp:TextBox ID="txtvalue" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="save" Width="91px" />
    &nbsp;
        <asp:Button ID="Button2" runat="server" Text="GoTo Form2" />
    </asp:Content>
     
  • Now add a another webform and create two Labels on it. First Label with Text Stored Value and second as it is. the form will display like below.

    Session2.gif
     
  • Write the below code on webform2.

        
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Label2.Text = Session("VALUE1")
        End Sub
     
  • The WebForm2.aspx looks like below.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="session.WebForm1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <
    body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Stored value"></asp:Label>
    &nbsp;<br />
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>   
        </div>
        </form>
    </body>
    </
    html>


Output:-

Session3.gif

Session4.gif

Session5.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.