views:

53

answers:

3

I'm new to asp world, and I have to keep my new job :) Switching form php to asp.net 3.5 (never used before). What would be the best practice for storing a SESSION variable in my project ?

How can I prevent my SESSION to be overwritten if my initialisation code is in the onPageLoad method of my MasterPage ?

My variables keeps beeing overwritten, please someone help me and tell me if there is any other solution than dealing with this pageLoad problem.

A: 
if(Session["account"] == null) {
Session["account"] = GetAccount();
}
ifwdev
Seems so obvious but truely works ! I heard that Global.asax could do the trick too , I think i'll have to dig there
Darkyo
A: 

You can always check that if the Session value exists or not.

if(Session["UserName"] == null) 
{
   Session["UserName"] = User.Identity.UserName; 
}
azamsharp
+1  A: 

First up, if your session values are getting lost... there must be something wrong. That totally defeats the purpose of having sessions!

You can try a couple of articles and get better understanding of this...

http://aspalliance.com/1182

http://blogs.msdn.com/rahulso/archive/2007/01/17/troubleshooting-cookies-a-case-study.aspx

HTH

Rahul Soni
After a few months I truely admit that you're right !
Darkyo
My pleasure Darkyo! Did you find the root cause?
Rahul Soni
Yes, another thread in Paradise...
Darkyo