Solved

Cleaning up Session Variables

Posted on 2004-08-12
4
213 Views
Last Modified: 2010-04-15
Hi All,

I have a question about session variables ...

I know that they are basically the best way of securely communicating between the diffrent pages of the application, but I know that since they sit on the server, they take up memory.

Is there a way of clearing the memory of sesion variables ?

eg: Session["FIRST"] = "Hello World";
Session["Second"] = 3.141527;

Now if I want to clear them what do I do ...

I have tried setting to "" and 0 .... and also to setting both to null ... but I'm not sure if that is enough ...
0
Comment
Question by:Pete2003
4 Comments
 
LVL 24

Expert Comment

by:Justin_W
ID: 11788464
Use:
   Session.Remove("Second")
or
   Session.Clear()
or
   Session.Abandon()
0
 
LVL 9

Expert Comment

by:glsac
ID: 11789953
Session.Abandon() <--- use this one
0
 

Author Comment

by:Pete2003
ID: 11791144
Wan't the Session.Abandon() clear all my session variables ??

I want to be able to clear specific ones and not the whole thing ...
0
 
LVL 1

Accepted Solution

by:
thefallguy earned 400 total points
ID: 11793168
Session.Abandon() will destroy your session as will Session.Clear(). You can use Session.Contents.Remove(Session["Second"]) to remove the variable from the collection of information about the current session.

Take this example. I have 3 session variables and i want to remove only the ones starting with SW. It is in VB .NET.

Session("SW.1")="Test 1"
Session("SW.2")="Test 2"
Session("Other")="Other"

For Each SessionItem in Session.Contents
  If Left(SessionItem,3) = "SW." then
    Session.Contents.Remove(SessionItem)
  end if
Next
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now