articles

Session in C#

Tom Cruser4002 06-May-2016

When we are working with an application on your computer, you open it, do some changes and then we close it. This is much like a Session. The computer knows who you are. It knows when we open the application and when we close it. However, on the internet there is one problem: the web server does not know who we are and what we do, because the HTTP address doesn't maintain state.

ASP solves this problem by creating a unique cookie for each user. The cookie is sent to the user's computer and it contains information that identifies the user. This interface is called the Session object.

The Session object stores information about, or change settings for a user session.

Variables stored in Session object hold information about one single user, and are available to all pages in one application. Common information stored in session variables is name, id, and preferences. The server creates a new Session object for each new user, and destroys the Session object when the session expires. 

Example: below provided a complete source code on Session.

LoginSession.aspx file

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="LoginSession.aspx.cs"Inherits="LoginSession"%>
   
<!DOCTYPEhtml>
 
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
    <title></title>
    <linkhref="StyleSheet.css"rel="stylesheet"type="text/css"/>
</head>
<body>
    <formid="form1"runat="server">
        <divalign="center"style="background-color: azure">
            <h2style="background-color: chocolate; font-family: Cambria; text-align: center">Login Page<hr/></h2>
            <tableborder="0">
                <tr>
                    <td>Name: </td>
                    <td>
                        <asp:TextBoxID="txtUserName"runat="server"/>
                    </td>
                </tr> 
                <tr>
                    <td></td>
                </tr>
                <tr> 
                    <td></td>
                </tr> 
                <tr> 
                    <td>
                        <asp:ButtonID="btnLogin"runat="server"Text="Login"OnClick="btnLogin_Click"CssClass="btn"Width="100px"ForeColor="Tomato"BorderStyle="Ridge"/>
                    </td>
                    <td></td>
                </tr> 
            </table>
            <hr/>
            <asp:LabelID="lblstatus"runat="server"/> 
        </div>
    </form>
</body>
</html>

LoginSession.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
publicpartialclassLoginSession : System.Web.UI.Page
{
    protectedvoid Page_Load(object sender, EventArgs e)
    { 
    }
    protectedvoid btnLogin_Click(object sender, EventArgs e)
    {
        // Create new session variable named "username"
        Session["username"] = txtUserName.Text;
        Response.Redirect("SessionStart.aspx"); 
    }
}


SessionStart.aspx file

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="SessionStart.aspx.cs"Inherits="SessionStart"%>
 
<!DOCTYPEhtml>
 
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
    <title></title>
</head>
<body>
    <formid="form1"runat="server">
    <div>
        <asp:labelID="lbl1"runat="server"/>
       
     
   
    </div>
    </form>
</body>
</html>


SessionStart.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
publicpartialclassSessionStart : System.Web.UI.Page
{
    protectedvoid Page_Load(object sender, EventArgs e)
    { 
        if (Session["username"] != null)
        {
            lbl1.Text = "Welcome " + Session["username"].ToString();
        } 
    }
}

Updated 27-Mar-2018

Leave Comment

Comments

Liked By