How to use Session in ASP.net with Example

random

How to use Session in ASP.net with Example

Session in ASP.net with C#

In previous article we learned about asp.net session, session id, session state types in asp.net. In the following example we will learn how to use asp.net session state to store and retrieve values of a user. Find the C# code below :-

Related Articles
session in asp.net c# with example,session in asp.net with example code project,view state in asp.net with example,session in asp.net using c#,session in asp net using c sharp,cookies in asp net example
Session in asp net using c sharp
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Session in asp net using c sharp</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h3>How to use session in asp.net with example</h3>
            <div>
                Enter Userame:<br />
                <asp:TextBox ID="txtUserName" runat="server" MaxLength="50"></asp:TextBox><br />
                <br />
            </div>
            <div>
                <asp:Button ID="btnCreateSession" runat="server"
                    Text="Click to Create Session" OnClick="btnCreateSession_Click" />
                &nbsp;
              
                    <asp:Button ID="btnRetrieveSession" runat="server" CausesValidation="false"
                        Text="Click to Retrieve Session Value" OnClick="btnRetrieveSession_Click" />
                &nbsp;
              
                    <asp:Button ID="btnRemoveSession" runat="server" CausesValidation="false"
                        Text="Click to Remove Session Value" OnClick="btnRemoveSession_Click" />
                &nbsp;
              
                    <asp:Button ID="btnRemoveAll" runat="server" CausesValidation="false"
                        Text="Click to Remove All Sessions" OnClick="btnRemoveAll_Click" />
            </div>
            <p>
                <b>Note</b>: First create a session by providing user name in text field, then you can retrieve the value from session.
          
            </p>
            <div>
                Value stored in Session:
              
                    <strong>
                        <asp:Label ID="lblSessionValue" ForeColor="OrangeRed" runat="server"></asp:Label></strong>
            </div>

        </div>
    </form>
</body>

</html>

Default.aspx.cs
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 btnCreateSession_Click(object sender, EventArgs e)
    {
        Session["Username"] = txtUserName.Text.Trim();
        txtUserName.Text = string.Empty;
    }

    protected void btnRetrieveSession_Click(object sender, EventArgs e)
    {
        DisplaySessionValue();
    }

    protected void btnRemoveSession_Click(object sender, EventArgs e)
    {
        Session.Remove("Username");

        DisplaySessionValue();
    }

    protected void btnRemoveAll_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();

        DisplaySessionValue();
    }

    private void DisplaySessionValue()
    {
        if (Session["Username"] != null)
            lblSessionValue.Text = Convert.ToString(Session["Username"]);
        else
            lblSessionValue.Text = "No Value has been stored in session";
    }

}
I hope you will enjoy the development tips while programming with asp.net using C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. Also If you like this article, don't forget to share this article with your friends and colleagues.
How to use Session in ASP.net with Example Reviewed by Ravi Kumar on 1:21 AM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.