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

How to clear Session state in ASP.NET C#

Dear viewer's in this tutorial I will show how to clear session in asp.net c #. There are various way to clear session .For clear session we can use Clear() and Abadon() methods. When we want to clear occupied memory location then we need to use Session.Abandon methods. But When we want to clear only Session values then we need to use Session.Clear() Methods.if we destroy particular session we need to use Remove Method.

Clear 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 Clear Example

        </h2>

        <fieldset style="width: 40%">

            <legend>User Login</legend>

            <table width="100%" bgcolor="#CC6600" 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" />

                        <asp:Button ID="btnCheckSession" runat="server" Text="Check Session"

onclick="btnCheckSession_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");

    }

    protected void btnCheckSession_Click(object sender, EventArgs e)

    {

        Response.Redirect("DisplaySession.aspx");

    }

}

Dispaly Login Page (HTML)

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

<head runat="server">

    <title>Welcome to Clear Session Example</title>

    <style type="text/css">

        .style1

        {

            width: 150px;

        }

    </style>

</head>

<body>

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

    <div>

        <h2>

            Destroy Session and Session Value

        </h2>

        <fieldset style="width: 40%">

            <legend>Display User Login Information</legend>

            <table width="100%" bgcolor="#CC6600" 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 colspan ="3">

                      

                        <asp:Button ID="btnLogoutAbandon" runat="server" Text="Logout Using Abdon()" onclick="btnLogoutAbandon_Click"

                            />

                        <asp:Button ID="btnLogoutClear" runat="server" Text="Logout Using Clear()" onclick="btnLogoutClear_Click"

                            />

                        <asp:Button ID="btnLogoutRemove" runat="server" Text="Logout Using Remove() " onclick="btnLogoutRemove_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 DisplaySession : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

        if (Session["UserName"] != null && Session["Password"] != null)

        {

 

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

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

        }

        else

        {

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

            lblUserName.Text = string.Empty;

        }

    }

  

 

    protected void btnLogoutAbandon_Click(object sender, EventArgs e)

    {

        //For occupied memory location and Value

 

        if (Session["UserName"] != null && Session["Password"] != null)

        {

 

            //For Clear Particul Value

            Session.Abandon();

            Response.Redirect("Default.aspx");

 

        }

 

 

    }

    protected void btnLogoutClear_Click(object sender, EventArgs e)

    {

        //For Clear Only Session Values   

        if (Session["UserName"] != null && Session["Password"] != null)

        {

            //For Clear Particul Value

            Session.Clear();

            Response.Redirect("Default.aspx");

 

        }

 

    }

    protected void btnLogoutRemove_Click(object sender, EventArgs e)

    {

        if (Session["UserName"] != null && Session["Password"] != null)

        {

 

            //For Clear Particul Value

            Session.Remove("UserName");

            Response.Redirect("Default.aspx");

        }

       

      

    }

}

 

Out Put

Remove_Session_in_asp.Net_C_Sharp class=
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