What is Session?

We know HTTP is stateless protocol; this protocol cannot store user information on page because Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values. ASP.NET Session state enables you to store and retrieve values for a user when user navigates other ASP.NET pages in a Web application. A session is defined as the period of time that a unique user interacts with a Web application. Session provides us facility to store information on server side. Session data is stored as per user basis.

Some Session methods are:

·         Session.Abandon() - Removes the Session and all items that it contains

·         Session.RemoveAll() - Removes all items from the Session

·         Session.Remove ("SessionName") - Removes the item that stored under the name "SelectedName".

·         Session.Clear() - Removes  items from the Session

In this example I will show you that how to use session variable to store user information as well as we can show that how to retrieve values from session variable. 

Code
<form id="form1" runat="server">
    <table border="1" style="width: 300px; height: 200px; left: 450px; top: 250px; position: absolute">
        <tr>
            <td>
                <table style="width: 300px; height: 200px;  " cellpadding="2">
                    <tr>
                        <td colspan="2" align="center">
                            <asp:Label ID="UserDetail" runat="server" Text="Enter Inforamtion"
                                Font-Bold="True"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <asp:Label ID="Name" runat="server" Text="Name"> </asp:Label>
                        </td>
                        <td align="center">
                            <asp:TextBox ID="NameTextbox" runat="server" Width="105px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <asp:Label ID="City" runat="server" Text="City"> </asp:Label>
                        </td>
                        <td align="center">
                            <asp:TextBox ID="CityTextBox" runat="server" Width="105px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <asp:Label ID="Age" runat="server" Text="Age"> </asp:Label>
                        </td>
                        <td align="center">
                            <asp:TextBox ID="AgeTextBox" runat="server" Width="105px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <asp:Label ID="State" runat="server" Text="State"> </asp:Label>
                        </td>
                        <td align="center">
                            <asp:DropDownList ID="DropDownList1" runat="server" Width="105px">
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" colspan="2">
                            <asp:Button ID="Submit" runat="server" Text="Submit" onclick="Submit_Click" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </form>
Code
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] state = { "U.P.", "Delhi", "Chennai", "Kolkata" };//creating string array to store state name
            for (int c = 0; c < state.Length; c++)//executing loop
            {
                DropDownList1.Items.Add(state[c]);//state name adding in dropdownlist
            }
        }
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        Session["Name"] = NameTextbox.Text;//value storing in session variable
        Session["City"] = CityTextBox.Text;
        Session["Age"] = AgeTextBox.Text;
        Session["State"] = DropDownList1.Text;
        Response.Redirect("Userinforamtiom.aspx"); 
    }
}
Output

Session in ASP.Net

Code
<form id="form1" runat="server">
     <table border="1" style="width: 300px; height: 200px; left: 450px; top: 250px; position: absolute">
        <tr>
            <td>
                <table style="width: 300px; height: 200px;  " cellpadding="2">
                    <tr>
                        <td colspan="2" align="center">
                            <asp:Label ID="UserDetail" runat="server" Text="User Detail" Font-Bold="True"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <asp:Label ID="Name" runat="server" Text="Name"> </asp:Label>
                        </td>
                        <td align="center">
                            <asp:Label ID="UserName" runat="server" ></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <asp:Label ID="City" runat="server" Text="City"> </asp:Label>
                        </td>
                        <td align="center">
                            <asp:Label ID="UserCity" runat="server" ></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <asp:Label ID="Age" runat="server" Text="Age"> </asp:Label>
                        </td>
                        <td align="center">
                           <asp:Label ID="UserAge" runat="server" ></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <asp:Label ID="State" runat="server" Text="State"> </asp:Label>
                        </td>
                        <td align="center">
                           <asp:Label ID="UserState" runat="server" ></asp:Label>
                        </td>
                    </tr>
                  
                </table>
            </td>
        </tr>
    </table>
    </form>
Code 
public partial class Userinforamtiom : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserName.Text = Session["Name"].ToString();//retrieving value from session variable
        UserCity.Text = Session["City"].ToString();
        UserAge.Text = Session["Age"].ToString();
        UserState.Text = Session["State"].ToString();
    }
}

In this page user details will be shown by session variable.

  output

Session in ASP.Net


Leave Comment