articles

Home / DeveloperSection / Articles / Session in ASP.Net

Session in ASP.Net

Sachindra Singh8689 14-Feb-2011
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
<formid="form1"runat="server">
    <tableborder="1"style="width: 300px; height: 200px; left: 450px; top: 250px; position: absolute">
        <tr>
            <td>
                <tablestyle="width: 300px; height: 200px;  "cellpadding="2">
                    <tr>
                        <tdcolspan="2"align="center">
                            <asp:LabelID="UserDetail"runat="server"Text="Enter Inforamtion"
                                Font-Bold="True"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="center">
                            <asp:LabelID="Name"runat="server"Text="Name"></asp:Label>
                        </td>
                        <tdalign="center">
                            <asp:TextBoxID="NameTextbox"runat="server"Width="105px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="center">
                            <asp:LabelID="City"runat="server"Text="City"></asp:Label>
                        </td>
                        <tdalign="center">
                            <asp:TextBoxID="CityTextBox"runat="server"Width="105px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="center">
                            <asp:LabelID="Age"runat="server"Text="Age"></asp:Label>
                        </td>
                        <tdalign="center">
                            <asp:TextBoxID="AgeTextBox"runat="server"Width="105px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="center">
                            <asp:LabelID="State"runat="server"Text="State"></asp:Label>
                        </td>
                        <tdalign="center">
                            <asp:DropDownListID="DropDownList1"runat="server"Width="105px">
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="right"colspan="2">
                            <asp:ButtonID="Submit"runat="server"Text="Submit"onclick="Submit_Click"/>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </form>
Code
publicpartialclass_Default : System.Web.UI.Page
{
    protectedvoid 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
            }
        }
    }
    protectedvoid 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
<formid="form1"runat="server">
     <tableborder="1"style="width: 300px; height: 200px; left: 450px; top: 250px; position: absolute">
        <tr>
            <td>
                <tablestyle="width: 300px; height: 200px;  "cellpadding="2">
                    <tr>
                        <tdcolspan="2"align="center">
                            <asp:LabelID="UserDetail"runat="server"Text="User Detail"Font-Bold="True"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="center">
                            <asp:LabelID="Name"runat="server"Text="Name"></asp:Label>
                        </td>
                        <tdalign="center">
                            <asp:LabelID="UserName"runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="center">
                            <asp:LabelID="City"runat="server"Text="City"></asp:Label>
                        </td>
                        <tdalign="center">
                            <asp:LabelID="UserCity"runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="center">
                            <asp:LabelID="Age"runat="server"Text="Age"></asp:Label>
                        </td>
                        <tdalign="center">
                           <asp:LabelID="UserAge"runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <tdalign="center">
                            <asp:LabelID="State"runat="server"Text="State"></asp:Label>
                        </td>
                        <tdalign="center">
                           <asp:LabelID="UserState"runat="server"></asp:Label>
                        </td>
                    </tr>
                  
                </table>
            </td>
        </tr>
    </table>
    </form>
Code 
publicpartialclassUserinforamtiom : System.Web.UI.Page
{
    protectedvoid 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



Updated 07-Sep-2019

Leave Comment

Comments

Liked By