Click to See Complete Forum and Search --> : Having a global variable for whole web site
johnsonlim026
September 10th, 2007, 04:57 AM
Hi,
I am a new bee in asp.net.
I have a question to ask here.
Is it possible to have a global variable for database connection in asp.net which i can use for the whole web site and ended anywhere in the web site rather than when accssing the database i can a connection and closing the connection when finishing cessing the database?
Does anyone has idea on this?
Thanks
Shuja Ali
September 10th, 2007, 05:26 AM
How about using Web.Config file. After all Web.Config is accessible through out the ASP.NET project. And you can change or add whatever items you want to without re-compiling anything.
It is a common practice to put stuff like Database connections, file paths, etc in web.config file.
andreasblixt
September 10th, 2007, 05:35 AM
Altering the Web.config file will trigger a restart of the web application. If you want a global variable that is changeable during runtime, using the Application collection or a static class could be viable alternatives:
// Store global value
Application["MyGlobalValue"] = 123;
public static class MyApp
{
private static int myGlobalValue;
public static int MyGlobalValue
{
get { return myGlobalValue; }
set { myGlobalValue = value; }
}
}
//...
MyApp.MyGlobalValue = 123;
Note that using the last example can introduce issues when multiple threads try to change the value at the same time, and you should look into making it thread-safe before using it.
johnsonlim026
September 10th, 2007, 05:41 AM
Hi shuja ali,
Nice to meet you.
I have put my connection string in web.config.
My problem is each time when i need to access to the database table , i have to open the connection and close the connection after finisshing access.I am thinking to have a connection opened once for the used of web site and it is closed only when the user log out, disposed.
This will result only one connection open for the whole web site and closed once.This should save up a lot or resources.Do you have any idea on this?
Thanks
Shuja Ali
September 10th, 2007, 05:50 AM
The usual way of accessing database is need based. You open a connection, keep it alive till you have to use and then close it. A scenario would be where you are doing a search page. Once the user selects the search criteria, you build a query, open a connection, get the data, put in a dataset and close the connection.
This way you are keeping your database accessible to other applications too.
johnsonlim026
September 10th, 2007, 06:00 AM
I agree on the way of accessing the database is need base.
Currently i find out no place in asp.net to hold my globally open connection like in module in vb.net which the variable in module are accesible for the whole project.Do you have any idea on this?
andreasblixt
September 10th, 2007, 06:04 AM
You can do it with a static class like I showed you in my post, but database connections in ASP.NET are optimized to be used like so:
SqlConnection conn = new SqlConnection(...);
conn.Open();
SqlCommand ...;
conn.Close();
johnsonlim026
September 10th, 2007, 06:14 AM
Nice to meet you,.I have created the class as suggested.Do you have any idea to make it become a global variable(Public ExpClass as new DALClass) so that i can open and close the database connection only once?
Shuja Ali
September 10th, 2007, 06:17 AM
Nice to meet you,.I have created the class as suggested.Do you have any idea to make it become a global variable(Public ExpClass as new DALClass) so that i can open and close the database connection only once?
As I wrote in my previous post, the database connection is not required to be kept open for the life time of the application. Just open it only when you need it.
andreasblixt
September 10th, 2007, 06:18 AM
Well, if you look into Global.asax files, they let you define Application_OnStart and Application_OnEnd events. In these events you could open and close the database connection, respectively. Your class would just be an ordinary static (Shared in VB) class that can hold a connection instance.
However, as I said, ASP.NET handles opening/closing of connections pretty well itself by not closing the database connection entirely and recycling it if there are many requests in a short period of time.
johnsonlim026
September 10th, 2007, 08:53 PM
Got it, thanks !!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.