Home > DeveloperSection > Articles > Session in C#

Session in C#


C# C#  Session 
Ratings:
0 Comment(s)
 601  View(s)
Rate this:

Session in C#


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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginSession.aspx.cs" Inherits="LoginSession" %>
   
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
        <div align="center" style="background-color: azure">
            <h2 style="background-color: chocolate; font-family: Cambria; text-align: center">Login Page <hr /> </h2>
            <table border="0">
                <tr>
                    <td>Name: </td>
                    <td>
                        <asp:TextBox ID="txtUserName" runat="server" />
                    </td>
                </tr> 
                <tr>
                    <td></td>
                </tr>
                <tr> 
                    <td></td>
                </tr> 
                <tr> 
                    <td>
                        <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" CssClass="btn" Width="100px" ForeColor="Tomato" BorderStyle="Ridge" />
                    </td>
                    <td></td>
                </tr> 
            </table>
            <hr />
            <asp:Label ID="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; 
public partial class LoginSession : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        // Create new session variable named "username"
        Session["username"] = txtUserName.Text;
        Response.Redirect("SessionStart.aspx"); 
    }
}


SessionStart.aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SessionStart.aspx.cs" Inherits="SessionStart" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:label ID="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;
 
public partial class SessionStart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        if (Session["username"] != null)
        {
            lbl1.Text = "Welcome " + Session["username"].ToString();
        } 
    }
}

Don't want to miss updates? Please click the below button!

Follow MindStick