Rule of thumb for clearing Session?

eramgarden

Contributor
Joined
Mar 8, 2004
Messages
579
I wonder if I should be doing this or not...

In some of my pages, I hold, for exmple, 10-15 temp values in session vars.

After the user clicks on another link,data is saved, or when user is done with that page...should I clear those temp vars from session?? Or should I just clear all the session when user clicks logout?
 
You can let them here so you can use them further if needed. When they log out (Session.Abandon) all temp value are cleared automaticly.
 
Well... it depends... for exemple... when I add some information to a DB... once I'm done with all the info... I simply clear them from the Session...

But Logon information as Username ..... I keep them till the end.

The main question is there : will you use this information elsewhere ? is it essential in other pages ?
 
If the information you're saving in session variable are used in one page only, I suggest using viewstate instead, it gets cleared as soon as you leave the page...
 
Yeah... of course...

I was more talking about a request that extend to more than 1 page. Like a registration... On a page it ask 4 to 5 info then after 3 to 4 ... and so on... until the end. They are usually regrouped by subject (Personal info, Billing info, etc...). This is the case where I store all the value in Session but once at the end... I clear them all once the request have been registered succesfully.

Using Session for usage in 1 page isn't needed... unless there's a very specific case.
 
yeah, light bulb is much brighter...2 more questions...

1. I havent come across a good website about Sessions and explaination. Do u guys have one?

2. I sometimes do this...it works but wondering if this is the right way or if i even need to do it...

At the top of my page, i have

Session("whatever")=string.Empty

this is to reset my session var ..then i save data or whatever and set that Session("whatever")="Y". I use this value in another page .

Do i need to reset my session var to blank to clear it ..I do it because i want to execute code in the other page ONLY if that session value is set to "Y" ...

Thanks for the help
 
Back
Top