473,480 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Remove all session keys except one

Hi!

I want to basicaööy run a Session.Abandon() on logout but keep one session.
In order to do this I'm iterating through my session collection by runing
the follwoing code:

Dim iSessionCount As Integer = Session.Count - 1

For i As Integer = 0 To iSessionCount

If Session.Contents.Keys(i) <> "ReturnPath" Then

Session.Remove(Session.Keys(i))

End If

Next

The statement works fine, but since I use the remove-statement, the index
decrease by 1 for each session I remove. So, let's say I have 5 sessions. i
run my code and iSessionCount will be 4 all through the loop. BUT, after the
first removal, the session index count will be 3. As a result of this I will
end up in an out-of-index error message.

Any ideas? I've been googling around but can only find solutions for the
Classic ASP-world and I want to do it the .NET-way.

Any help is appreciated.

Thanks in advacne,
Fred
Nov 18 '05 #1
4 3454
Fred,
I think you're making this one too complicated. Instead of starting
at zero, why not start at the last session and work your way back down to
zero. That way you're always popping the last one off.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Fredrik Rodin" <no**********@dacsa.n.o.s.p.a.m.net> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Hi!

I want to basicaööy run a Session.Abandon() on logout but keep one session. In order to do this I'm iterating through my session collection by runing
the follwoing code:

Dim iSessionCount As Integer = Session.Count - 1

For i As Integer = 0 To iSessionCount

If Session.Contents.Keys(i) <> "ReturnPath" Then

Session.Remove(Session.Keys(i))

End If

Next

The statement works fine, but since I use the remove-statement, the index
decrease by 1 for each session I remove. So, let's say I have 5 sessions. i run my code and iSessionCount will be 4 all through the loop. BUT, after the first removal, the session index count will be 3. As a result of this I will end up in an out-of-index error message.

Any ideas? I've been googling around but can only find solutions for the
Classic ASP-world and I want to do it the .NET-way.

Any help is appreciated.

Thanks in advacne,
Fred

Nov 18 '05 #2
The usual trick is to browse the collection starting by the end :

For i=Session.Count-1 to 0 step -1
If ...
Next
Patrice

"Fredrik Rodin" <no**********@dacsa.n.o.s.p.a.m.net> a écrit dans le message
de news:%2***************@TK2MSFTNGP12.phx.gbl...
Hi!

I want to basicaööy run a Session.Abandon() on logout but keep one session. In order to do this I'm iterating through my session collection by runing
the follwoing code:

Dim iSessionCount As Integer = Session.Count - 1

For i As Integer = 0 To iSessionCount

If Session.Contents.Keys(i) <> "ReturnPath" Then

Session.Remove(Session.Keys(i))

End If

Next

The statement works fine, but since I use the remove-statement, the index
decrease by 1 for each session I remove. So, let's say I have 5 sessions. i run my code and iSessionCount will be 4 all through the loop. BUT, after the first removal, the session index count will be 3. As a result of this I will end up in an out-of-index error message.

Any ideas? I've been googling around but can only find solutions for the
Classic ASP-world and I want to do it the .NET-way.

Any help is appreciated.

Thanks in advacne,
Fred

Nov 18 '05 #3
Thanks to Mark Fitzpatrick and Patrice!

It did solve my problem to reverse the loop.

Thanks,
Fred

"Patrice" <no****@nowhere.com> wrote in message
news:OR*************@TK2MSFTNGP10.phx.gbl...
The usual trick is to browse the collection starting by the end :

For i=Session.Count-1 to 0 step -1
If ...
Next
Patrice

"Fredrik Rodin" <no**********@dacsa.n.o.s.p.a.m.net> a écrit dans le message de news:%2***************@TK2MSFTNGP12.phx.gbl...
Hi!

I want to basicaööy run a Session.Abandon() on logout but keep one session.
In order to do this I'm iterating through my session collection by runing the follwoing code:

Dim iSessionCount As Integer = Session.Count - 1

For i As Integer = 0 To iSessionCount

If Session.Contents.Keys(i) <> "ReturnPath" Then

Session.Remove(Session.Keys(i))

End If

Next

The statement works fine, but since I use the remove-statement, the index decrease by 1 for each session I remove. So, let's say I have 5

sessions. i
run my code and iSessionCount will be 4 all through the loop. BUT, after

the
first removal, the session index count will be 3. As a result of this I

will
end up in an out-of-index error message.

Any ideas? I've been googling around but can only find solutions for the
Classic ASP-world and I want to do it the .NET-way.

Any help is appreciated.

Thanks in advacne,
Fred


Nov 18 '05 #4
You could also set the session variable to a temporary value, clear the session, then reset it, such as

object returnPath = Session["ReturnPath"

Session.Clear(
Session.Abandon(

Session["ReturnPath"] = returnPath

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
24594
by: MFRASER | last post by:
How do I go about looping through a hash table and removing items. I know how do this in a collectionbase, but can't iterate through the hash table with out getting an error. Here is my sample...
4
2037
by: Cowboy \(Gregory A. Beamer\) | last post by:
Background: ------------- The idea started as a single sign on type of application. Having tested it before, I knew we could institute single sign on using the same Authentication Cookie name (in...
8
1669
by: Tim W. | last post by:
Chaps. We are working with SQL-Server based Sessions in the following environemtn: Win-Server 2K3 incl. IIS, Dot Net Framework 1.1, SQL2K SP3a, German. We are defining a Session-Variable...
1
1788
by: Kevin Jackson | last post by:
Hello, Using any of the ASP.NET session storage techniques, is there a method in one of the HTTP objects that you can get the list of active (not abandoned or timed out) session keys (and maybe...
2
4554
by: eight02645999 | last post by:
hi i am using a telnet session to simulate an authentication mechanism USER = "user" PASSWORD = "password" try: telnet = telnetlib.Telnet(HOST) telnet.set_debuglevel(5)...
1
5621
by: Hans Kesting | last post by:
Hi, Is it possible to share sessions between subdomains? Say: the user logs in at www.company.com, and is redirected to my.company.com. This is a different url for the same application. Can I...
6
7157
by: tshad | last post by:
Is there a reason to use session.remove over session.contents.remove? Don't they both remove the key and data from the contents collection? I assume that session(x) = nothing does essentially...
8
2560
by: bmukulu | last post by:
Hi, I am trying to add some error handling in a Global.asax file. I am declaring a session variable within the Application_Error procedure. However, everytime i try to pass something into the...
2
1481
by: Nathan Sokalski | last post by:
I have an application that is using Session state. On one page, I use the Session.Add() method to create the Key/Value pair for the first time. I am then sent to another page using...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.