Posted by : Anwar Hossain
Category : State Management in ASP C #

Session state in ASP.NET C#

Dear viewer's in this tutorial I will explain about session state in ASP.NET’ C #. Session allows to store any type of data in memory on ther server.Session Information is more Proted and reliable for security purpose .Session information is not transferred to the client and it can be identified uniquely for individual client.Best example of session is shopping cart. The most important thing to remember that session value is always move one page to another page as necessary. In this tutorial I have taken two pages one for inputting session value and another for displaying session value. When user click user Log In the User Name and Password will be stored to the sessions and displayed another page.

Session state in ASP.NET C#

HTML

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Welcome to Session Example</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <h2>

            Welcome to Session Example

        </h2>

        <fieldset style="width: 40%">

            <legend>User Login</legend>

            <table width="100%" bgcolor="#939300" cellpadding="2" cellspacing="2">

                <tr>

                    <td width="150" align="right">

                        User Name

                    </td>

                    <td width="10">

                    </td>

                    <td>

                        <asp:TextBox ID="txtUserName" runat="server" Width="250px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td width="150" align="right">

                        Password

                    </td>

                    <td width="10">

                        &nbsp;

                    </td>

                    <td>

                        <asp:TextBox ID="txtPasswordName" runat="server" Width="250px"

                            TextMode="Password"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td width="150" align="right">

                        &nbsp;

                    </td>

                    <td width="10">

                        &nbsp;

                    </td>

                    <td>

                        <asp:Button ID="btnSubmit" runat="server" Text="Submit"

                            onclick="btnSubmit_Click" />

                    </td>

                </tr>

            </table>

        </fieldset>

    </div>

    </form>

</body>

</html>

 

CODE BEHIND

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        Session["UserName"] = txtUserName.Text.Trim();

        Session["Password"] = txtPasswordName.Text.Trim();

 

        Response.Redirect("DisplaySession.aspx");

    }

}

 

Dispaly Login Page (HTML)

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Welcome to Session Example</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <h2>

            Display Session Value

        </h2>

        <fieldset style="width: 40%">

            <legend>Display User Login Information</legend>

            <table width="100%" bgcolor="#B4B48D" cellpadding="2" cellspacing="2" style="color: #FFFFFF">

                <tr>

                    <td width="150" align="right" valign="middle">

                        User Name

                    </td>

                    <td width="10" valign="middle">

                        :

                    </td>

                    <td>

                        <asp:Label ID="lblUserName" runat="server" Text=""></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td width="150" align="right">

                        Password

                    </td>

                    <td width="10" valign="middle">

                        :

                    </td>

                    <td>

                        <asp:Label ID="lblPassord" runat="server" Text=""></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td width="150" align="right">

                        &nbsp;

                    </td>

                    <td width="10">

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

            </table>

        </fieldset>

    </div>

    </form>

</body>

</html>

 

CODE BEHIND

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class DisplaySession : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        lblUserName.Text = Session["UserName"].ToString();

        lblPassord.Text = Session["Password"].ToString();

    }

}

 

Out Put

Csharp_Session_UserName_Password
Csharp_Session_UserName_Password_Display



Realted Article Headline

How to Pass Query String parameter From DataList
Query String Example in ASP.NET C#
How to increase session time in ASP.Net C#
stroe datatable in session retrive display session vlaues in ASP.Net C #
How to clear Session state in ASP.NET C#
Session state in ASP.NET C#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection