ASP.Net 2.0 Static members

D

devg

I want to have a global variable in my app so I can change my DB connect
string for all the pages based on the person's login information. I have a
class that has a static string. I can get/set that from any page and it
works good. ***Is it OK to use statics in this manner or is there a chance
that a different user will affect the static members that yet another user
is using (the whole life cycle for internet apps thing).

I have it running and it works fine but I wonder...

I have seen lots of posts on this but many conflict so I am asking here for
clarification.

Thanks you!
 
E

Eliyahu Goldin

No, this is not correct.

Static members are shared between user sessions. Every new user will
overwrite them.

You should use session variables.

Eliyahu
 
D

devg

Since I just need to store a simple string to be available to all pages I
would love to use Session Variables but I have been hesitent since I have
seen many posts saying this is not good practice. I am using ASP.NET 2.0 so
I wonder if using Session Vars in this way is OK now (compared to classic
ASP which I assume it was not good)?
Thank you!
 
E

Eliyahu Goldin

I can't think of anything bad in using session variables for your purpose.

Eliyahu
 
S

Steven Cheng[MSFT]

Hi Devg,

I agree with Eliyahu that for your scenario, use SessionState variable is
the preferred approach. Static class member can only store data which will
be shared among the whle application(Appdomain). And for sessionState, if
you don't store two much data in it, that should be ok.

Please feel free to post here if you have any other consideration or
concerns.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

devg

Thank you Eliyahu and Steven for your info!
I see clearly that static is not the way to go now.
I am concerened that I have seen so many negative articles on using Session
(and I am so used to not using them from classic ASP!) that I wonder if
there is a better way? I suppose Session would be ok for just a string or
two but I am also wondering if perhaps putting the data in the
authentication ticket would be good too (so I would have a role for each
user that would point to what DB they were to use).
Thanks for all your ideas and info -- what do you think about using UserData
in the ticket vs. Session?
 
S

Steven Cheng[MSFT]

Thank you for the response Devg,

Authentication cookie(ticket) is also ok, and the difference between
sessionstate is that authentication ticket store in client-side cookie, so
if we store to much data , it'll increase the data tranferred between
client server(though we won't store much data in it generally ;)). BTW, I
just forget the mention the user profile in ASP.NET 2.0, have you ever
considered to use this as the storage?

#ASP.NET Profile Properties Overview
http://msdn2.microsoft.com/en-us/library/2y3fs9xs(VS.80).aspx

#Storing User Profiles
http://www.asp.net/QuickStart/aspnet/doc/profile/default.aspx

Profile service is used to help store user specific data and the default
storage is the database(sqlserver).

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top