Solved

Cleaning up Session Variables

Posted on 2004-08-12
4
217 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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