Darkyo
Darkyo

Reputation: 83

How can I prevent my SESSION from being overwritten on pageLoad of my MasterPage?

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.

Upvotes: 1

Views: 1390

Answers (3)

azamsharp
azamsharp

Reputation: 20086

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

if(Session["UserName"] == null) 
{
   Session["UserName"] = User.Identity.UserName; 
}

Upvotes: 0

Rahul Soni
Rahul Soni

Reputation: 4968

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

Upvotes: 1

kwcto
kwcto

Reputation: 3504

if(Session["account"] == null) {
Session["account"] = GetAccount();
}

Upvotes: 0

Related Questions