Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cleaning up Session Variables

Posted on 2004-08-12
4
Medium Priority
?
223 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 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

876 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