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.
In previous articles I explained What is Session and Difference between cookie and session?,what is session? How to use session in ASP.NET?,Is it possible to set the session out time manually in ASP.NET? || How to set session timeout more than 20 min.? || How to set session timeout in web.config file?
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;
}
}
|
No comments:
Post a Comment