Solved

Cleaning up Session Variables

Posted on 2004-08-12
4
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET - Why is accordion not working? 3 72
C# LINQ 5 43
c# deserialize JSON from web service using JsonConvert 9 59
What are MicroServices? 4 67
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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