Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 17, 2008 03:35 PM by Jason Duncan
Apr 17, 2008 08:32 AM|LINK
I need to inspect all of my existing Session Variables. How can I accomplish this in C#?
I see that VB has a handy "Session.Contents" collection that can be looped through. I need something similar in C#, but I have not been able to find a similar solution.
Any help would be appreciated.
Apr 17, 2008 09:31 AM|LINK
I think something like either of these should work:
for (int i = 0; i < Session.Contents.Count; i++)
Response.Write(Session.Keys[i] + " - " + Session[i] + "<br />");
foreach (string key in Session.Keys)
Response.Write(key + " - " + Session[key] + "<br />");
Apr 17, 2008 09:35 AM|LINK
In C# you can loop through Session using Session.Count that will give you max count now you can use this count in your for loop
or you can use Session.Keys that will return you all keys which are present in current session
Apr 17, 2008 03:10 PM|LINK
Thanks! Works perfectly.
Apr 17, 2008 03:23 PM|LINK
Actually, this works fine in the Code behind file--which solves my problem.
However, I was initially trying to access the Session object from a Business Logic Layer, and I did not see Session object available at that level. For example, in my BLL whenever I type in Session, the intellisense no longer gives me Session.Add, Session.Contents,
etc. Is there a way that I can access Session from someplace other than .aspx? I've looked at the various System includes and those don't seem to have any bearing on this issue.
I was hoping to have a BLL.Global.ClearSessionDataSetVars() that could be called from multiple places.
Just wondering (and thanks again),
Apr 17, 2008 03:27 PM|LINK
Apr 17, 2008 03:35 PM|LINK
Thanks again, that was it!