Friday, 30 August 2013

How to remove specific session in ASP.NET,How to remove all sessions in ASP.NET

Hi friends, in this article I will explain about how to remove the sessions in ASP.NET,How to remove specific session in ASP.NET.
Sessions plays a vital role in ASP.NET state management.

How to remove sessions in ASP.NET using VB.NET
To remove particular created session
Session.Remove("SessionName to remove")
or
Session("SessionName to remove") = Nothing

To remove all created sessions
Session.RemoveAll()
or
Session.Abandon()

How to remove sessions in ASP.NET using  C#.NET
To remove particular created session
Session.Remove("SessionName to remove");
or
Session["SessionName to remove"] = null;

To remove all created sessions
Session.RemoveAll();
or
Session.Abandon();

I will explain this one small example.
Take one webpage and take one label as shown below.
In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Aspdotnet-Kishore: how to remove session(s) in ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h4 style="color:red">Aspdotnet-Kishore:how to remove session(s) in ASP.NET</h4>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

In VB.NET:
Partial Class removesession
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("Sitename") = "Aspdotnet-Kishore.blogspot.com"
        Session("fbid") = "AspdotnetKishore"
        Session("twitterid") = "AspdotnetKishore"
        Label1.Text = "Reading Sessions...<br />"
        Label1.Text += "<br />Site Name :" + Session("Sitename")
        Label1.Text += "Facebook ID :" + Session("fbid")
        Label1.Text += "<br />Twitter ID :" + Session("twitterid")
        'removing sitename session
        Session.Remove("Sitename")
        Label1.Text += "<br /><br />Now remove the item [Sitename]"
        Label1.Text += "<br />Site Name :" + Session("Sitename")
        Label1.Text += "<br />Facebook ID :" + Session("fbid")
        Label1.Text += "<br />Twitter ID :" + Session("twitterid")
        'remove all the sessions
        Session.RemoveAll()
        Label1.Text += "<br /><br />Now remove the all sessions"
        Label1.Text += "<br />Site Name :" + Session("Sitename")
        Label1.Text += "<br />Facebook ID :" + Session("fbid")
        Label1.Text += "<br />Twitter ID :" + Session("twitterid")
    End Sub
End Class

In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class removesession : System.Web.UI.Page
{
                protected void Page_Load(object sender, System.EventArgs e)
                {
                                Session["Sitename"] = "Aspdotnet-Kishore.blogspot.com";
                                Session["fbid"] = "AspdotnetKishore";
                                Session["twitterid"] = "AspdotnetKishore";
                                Label1.Text = "Reading Sessions...<br />";
                                Label1.Text += "<br />Site Name :" + Session["Sitename"];
                                Label1.Text += "Facebook ID :" + Session["fbid"];
                                Label1.Text += "<br />Twitter ID :" + Session["twitterid"];
                                //removing sitename session
                                Session.Remove("Sitename");
                                Label1.Text += "<br /><br />Now remove the item [Sitename]";
                                Label1.Text += "<br />Site Name :" + Session["Sitename"];
                                Label1.Text += "<br />Facebook ID :" + Session["fbid"];
                                Label1.Text += "<br />Twitter ID :" + Session["twitterid"];
                                //remove all the sessions
                                Session.RemoveAll();
                                Label1.Text += "<br /><br />Now remove the all sessions";
                                Label1.Text += "<br />Site Name :" + Session["Sitename"];
                                Label1.Text += "<br />Facebook ID :" + Session["fbid"];
                                Label1.Text += "<br />Twitter ID :" + Session["twitterid"];
                }
    Public removesession()
                {
                                Load += Page_Load;
                }
}

 Happy Reading ........If you like my blog Aspdotnet-Kishore then why are you waiting like my blog through facebook page Aspdotnet-Kishore

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.