Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Asp.net session state - Part 62

Suggested Videos
Part 59 - QueryString in asp.net
Part 60 - Cookies in asp.net
Part 61 - How to Check if cookies are enabled or disabled

Different techniques to send data from one webform to another 
1. Cross Page Postback: Discussed in Part 55 and Part 56
2. Context.Handler object - Discussed in Part 58
3. Query strings - Discussed in Part 59
4. Cookies - Discussed in Part 60 and Part 61
5. Session state - Will be discussed in this session

The following concepts will be discussed in the subsequent sessions
6. Application state

Just like Query strings, Session State variables can also be used to send data from one webform to another. 



Points to remember about session state variables:
1. Session state variables are stored on the web server by default, and are kept for the life time of a session.
2. The default session state mode is InProc. We will discuss about different session state modes in a later video session.
3. The life time of a session is determined by the timeout value in web.config file. The default is 20 minutes. The time-out value can be adjusted according, to your application requirements.
<sessionState mode="InProc" timeout="30"></sessionState>
4. Session state variables are available across all pages, but only for a given single session. Session variables are like single-user global data.
5. It is always a good practice to check, if a session state variable is null before calling any of its methods, such as ToString(). Otherwise, we may run into runtime NullReferenceExceptions.
if (Session["Name"] != null)
{
    lblName.Text = Session["Name"].ToString();    
}
6. Application performance can be improved by disabling session state, if it's not required. Session state can be turned off at the page or application level.

To turn of the session state at the page level, set EnableSessionState="False" in the page directive
<%@ Page Language="C#" EnableSessionState="False" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AdoDemo.WebForm1" %>

To turn of the session state at the application level, set SessionState mode=false in web.config file.
<sessionState mode="Off"></sessionState>



WebForm1.aspx HTML source
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>
                This is WebForm1</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:TextBox ID="txtName" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:TextBox ID="txtEmail" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2"> 
            <asp:Button ID="btnSendData" runat="server" 
            Text="Go to WebForm2" onclick="btnSendData_Click" />
        </td>
    </tr>
</table>
</div>

WebForm1.aspx.cs code:
protected void btnSendData_Click(object sender, EventArgs e)
{
    Session["Name"] = txtName.Text;
    Session["Email"] = txtEmail.Text;
    Response.Redirect("WebForm2.aspx");
}

WebForm2.aspx HTML Source:
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>This is WebForm2</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:Label ID="lblName" runat="server">
            </asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:Label ID="lblEmail" runat="server">
            </asp:Label>
        </td>
    </tr>
</table>
</div>

WebForm2.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["Name"] != null)
    {
        lblName.Text = Session["Name"].ToString();    
    }
    if (Session["Email"] != null)
    {
        lblEmail.Text = Session["Email"].ToString();
    }
}

1 comment:

  1. Hi Venkat,

    if we increase the session time more than 20 it's not working because session timeout value shoud be <= application pool idle timeout property value,
    can we overcome this by code?

    Thank you!

    ReplyDelete

It would be great if you can help share these free resources