473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

static variables vs. session variables in ASP.NET

Hi,

I'm new to .NET and have a question about the use of static variables vs.
session variables in a web form in C#.

Instead of using a session variable to hold a string to persist during
datagrid paging. Can I use a static variable? Also, if I use a static
variable is that variable shared by everyone that brings up the aspx page?
Ex, if 4 people are viewing the aspx page is an instance of the static
variable created for each of the 4 people? Or is there just one instance of
that static variable shared across the 4 users?

BTW, I am using the session variable/static string to hold the value of some
filter criteria based on what buttons are pressed by the user which would hit
the database with different sql statements.

Thank you.

Nov 16 '05 #1
4 41960
There is only one copy of a static variable, so it is not an appropriate
place to put session-specific information.

Ken
"Wayne" <Wa***@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
Hi,

I'm new to .NET and have a question about the use of static variables vs.
session variables in a web form in C#.

Instead of using a session variable to hold a string to persist during
datagrid paging. Can I use a static variable? Also, if I use a static
variable is that variable shared by everyone that brings up the aspx page?
Ex, if 4 people are viewing the aspx page is an instance of the static
variable created for each of the 4 people? Or is there just one instance of that static variable shared across the 4 users?

BTW, I am using the session variable/static string to hold the value of some filter criteria based on what buttons are pressed by the user which would hit the database with different sql statements.

Thank you.

Nov 16 '05 #2
A static variable will be shared across all instances of the aspx page,
meaning that it is the same variable for every request that is coming in. I
don't think this is what you want because if you have 2 users, then their
actions will affect the view provided to the other user.

If this is a single page, you might consider using ViewState. This would
allow you to set a value for each of the users. However, this value would
only be good for the ASPX page they are on and only during postbacks. If
they leave and come back, the value won't be there anymore.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
"Wayne" <Wa***@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
Hi,

I'm new to .NET and have a question about the use of static variables vs.
session variables in a web form in C#.

Instead of using a session variable to hold a string to persist during
datagrid paging. Can I use a static variable? Also, if I use a static
variable is that variable shared by everyone that brings up the aspx page?
Ex, if 4 people are viewing the aspx page is an instance of the static
variable created for each of the 4 people? Or is there just one instance of that static variable shared across the 4 users?

BTW, I am using the session variable/static string to hold the value of some filter criteria based on what buttons are pressed by the user which would hit the database with different sql statements.

Thank you.

Nov 16 '05 #3
Correct -- there is only ever one instance of a static member. That also
means that if you use static data you need to ensure the code that accesses
that data is thread-safe. The Session object is where you want to put your
session-specific data.

Ken
"Wayne" <Wa***@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
So, if 4 people are accessing the aspx page then they are all sharing the
same instance of that static member?

"Ken Kolda" wrote:
There is only one copy of a static variable, so it is not an appropriate
place to put session-specific information.

Ken
"Wayne" <Wa***@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
Hi,

I'm new to .NET and have a question about the use of static variables vs. session variables in a web form in C#.

Instead of using a session variable to hold a string to persist during
datagrid paging. Can I use a static variable? Also, if I use a static variable is that variable shared by everyone that brings up the aspx page? Ex, if 4 people are viewing the aspx page is an instance of the static
variable created for each of the 4 people? Or is there just one
instance of
that static variable shared across the 4 users?

BTW, I am using the session variable/static string to hold the value
of some
filter criteria based on what buttons are pressed by the user which
would hit
the database with different sql statements.

Thank you.


Nov 16 '05 #4
Ben and Ken

Thank you guys very much for your help. I was trying to find a work around
for using session variables because for some reason the filter criteria I was
using on the datagrid was causing exceptions during postback (with paging
enabled). So I was trying to dump using Session variables and going with
Static variables. Now I see that's not the route to go.

Eventually, I set the web.config file with cookieless="true" and now the
paging is working correctly with my filter criteria. I'm going to play with
ViewStates as I'm interested in maintaining the state in just this one aspx
page. In either case, the viewstate/session variable holds a small string.

Once again, thank you!!!

"Ben Lucas" wrote:
A static variable will be shared across all instances of the aspx page,
meaning that it is the same variable for every request that is coming in. I
don't think this is what you want because if you have 2 users, then their
actions will affect the view provided to the other user.

If this is a single page, you might consider using ViewState. This would
allow you to set a value for each of the users. However, this value would
only be good for the ASPX page they are on and only during postbacks. If
they leave and come back, the value won't be there anymore.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
"Wayne" <Wa***@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
Hi,

I'm new to .NET and have a question about the use of static variables vs.
session variables in a web form in C#.

Instead of using a session variable to hold a string to persist during
datagrid paging. Can I use a static variable? Also, if I use a static
variable is that variable shared by everyone that brings up the aspx page?
Ex, if 4 people are viewing the aspx page is an instance of the static
variable created for each of the 4 people? Or is there just one instance

of
that static variable shared across the 4 users?

BTW, I am using the session variable/static string to hold the value of

some
filter criteria based on what buttons are pressed by the user which would

hit
the database with different sql statements.

Thank you.


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Earl Teigrob | last post by:
I am using C# to program ASP.NET. I am using class of static fields to hold all variables that are common to the application, and I am sure that this is fine. However, I was also using some static...
1
by: aliendolphin | last post by:
I'm having a strange problem possibly with Session variables for one user being accessed by another. I use a static utility class with static methods which get and set certain Session variables....
3
by: Mrs. Conni Drejer | last post by:
Please help anyone: I have made a DLL (for DBCommunication) in which I have defined some public static variables/fields in a public class. This because I want these variables to be shared in all...
4
by: Mantorok | last post by:
Hi I have an ASP app which references a few static properties in some of the classes. I understand that you should use Session variables, but is it "possible" to have each session "not"...
2
by: james | last post by:
Hi I am considering storing my session variables within one static object with session scope. The static object will be a class with accessor functions to get and set the equivalent session...
1
by: cobus.lombard | last post by:
I have found it very easy to have a Helper class that has static members which access Session Variables. For example: public class HelperClass { public static doCustomer CurrentCustomer { get...
1
by: nicepg | last post by:
Hi, i m new in C#, and i m trying to prgramm a calculator program in web application. My program work very well using a static variables but i need to change these variables to session...
5
by: dougloj | last post by:
Howdy. I have an ASP.NET application written in C#. I know this sounds nuts, but instead of having multiple .aspx files to display various parts of the Web site, I do almost everything on my...
4
by: =?iso-8859-1?B?Sm9oYW4gU2r2c3Ry9m0=?= | last post by:
It seems that many people experience problems with mix-ups and non-intentional sharing of sessions in ASP.NET. This is often tracked down to the use of static variables for user data, which are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.