We help IT Professionals succeed at work.

static variable in System.Web.UI.Page class in ASP.NET

engg asked
Last Modified: 2008-02-01
In C# ASP.NET web application,
I have a web form PageBase.aspx and it's code-behind page has this class.
public class PageBase : System.Web.UI.Page
static protected System.Data.Odbc.OdbcConnection odbcConn;
If I create a static variable in this class, will it be shared among different users of this application?
or will every web user have a separate copy of this variable?
Please help. Thanks.
Watch Question

Each request has its own context, so it will not be shared among separate requests, let alone among different users.

To share data between requests use the Session or Cache properties

To share data among different users use Application variables.
OK I was incorrect.  static variables _are_ shared among requests.  I'm not certain if they are shared among requests from different users, though.  I'm assuming that they hold their value as long as the underlying AppPool is running.

However, standard practice is to use the Session, Cache, and Application collections to share data.


Thanks dstanley9.
Could you suggest how I should be using the above variable 'odbcConn' ?


Should 'odbcConn' be static or not?
You should not share database connections as it would be very difficult to control the opening and closing of the connection, and you will most likely run into errors when two requests atempt to use the connection at the same time.  If the connection uses the same connection string (meaning the same database, username, and password) each time, then the connection will be "pooled" by the .NET framework and you will have better performance.


Thank you, dstanley9.
I have put the connection string in web.config and the same string is being used throughout the application.  All web forms in the application inherit the 'PageBase' class (defined above in the main question).  I plan to do something like this.
public PageBase()
   if (odbcConn == null)
               odbcConn = new System.Data.Odbc.OdbcConnection();
So if I don't have odbcConn as 'static',  for every web form page request, every time odbcConn is going to be null and it's always going to execute the 'new' statement. Is it very inefffcient to have it like that?
Yes, but making it static or sharing it in the Application collection is going to cause more harm than good.  I would not create the connection object until you need it.  I generally don't create a connection until I'm going to do the data acccess.  If you use the same connection string, then the connection will be pooled as I mentioned earlier, so it should be fairly light.


Thank you, dstanley9. So I am no longer going to have odbcConn as 'static'
So only when I do
I should be doing
                              odbcConn = new System.Data.Odbc.OdbcConnection();
Am I right?
Almost all web forms in my application open 'odbcConn'. So that's why I thought of executing the 'new' statement in their parent class 'PageBase'.  
Is this ok?
Is it ok to call new() in Page.Base?  I would not.  Even though it adds more code, it is much safer to just create the connection when you need it rather than trying to create it once and have to worry about multiple connections using it at the same time.  Don't worry about making it a property of the page class.  Just declare it and create it when you need it, and pull the conncetion string from web.config.  

Now that I think about it, though, you _could_ add a method to PageBase to get a connection:

protected OdbcConnection GetConnection()
  System.Data.Odbc.OdbcConnection odbcConn = new System.Data.Odbc.OdbcConnection({get connection string from web.config});
  return odbcConn;

then when you need a conection, just call that function:

using(OdbcConnection conn = Getconnection())
  OdbcCommand command = ...

the using() block will automatically close the connection when it exits, even if an exception is thrown.  That should be pretty clean.


Thanks dstanley9. I learnt about a lot other things from this answer.
One last thing - Does connection pooling come into effect when executing the following statement?

odbcConn = new System.Data.Odbc.OdbcConnection();

I mean does connection pooling in ASP.NET make this operation less expensive than what it would have been without connection pooling? (connection string is same everywhere and the connection is being closed properly every time)
Connection pooling occurs if you specify min and max pool in your connection string. not 100% sure of less or more expensive thing, what i know is if max pool is achieved a new pool is created. so you need to be aware how many connections you want to pool.
Connection pooling comes into play when you Open() a connection, not when the object is created.  When the object is created, everything is just in memory - there is no actual database connection yet (that happens at a lower level).  When you call Open(), the runtime will see if a connection is available in the pool and use it, otherwise it will create a new one.  If you're using the same connection string each time, you should be using a pooled connection most of the time.

Also, make sure you wrap your connections in a "using" block as shown above - that will automatically Close the connection, which will maake it available for use again.

Also, in response to the last post:


Connection pooling is on by default regardless of whether or not you specify the pool size.  the max pool size tells the runtime how many connections can exist in the pool (default is 100).  The Min Pool Size tells the runtime to create a certain number of connections upon its creation (default is 0 - connections are created as they are needed).  


Thanks dstanley9. You said <When you call Open(), the runtime will see if a connection is available in the pool and use it, otherwise it will create a new one. >

In your 'using' block, every time before you open a connection, you create it by calling 'new', so how is it using the pool when a connection is opened?
This one is on us!
(Get your first solution completely free - no credit card required)


Thank you very much, dstanley9.
All your comments were prompt,  very informative and helpful.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.