Click to See Complete Forum and Search --> : Create and close database connection
dummyagain
December 10th, 2006, 11:52 AM
I would like to set the database variable and use it for multiple pages.
Is it possible to set the variable in one page and used by other pages?
Thank you
jasonli
December 11th, 2006, 08:50 AM
Save the value in HttpContext.Current.Cache.
TheCPUWizard
December 11th, 2006, 09:07 AM
If you are talking about the connection object, you should always create, use, dispose as fast as possible. If you are using C#, then make use of the "using" statement!
dummyagain
December 11th, 2006, 01:40 PM
yes.. i think so.. so i keep every method open db conenction, executing query and the close it.... however each page will contain constant like username, password, db name...etc for connecting to db ..so i would like to know if i can save those data in one place..and when i change it..it can be done once but not page by page...
Thank you
Shuja Ali
December 12th, 2006, 01:47 AM
This is where your design skills will be needed. What you basically need to do is use a separate Data Layer in your ASP.NET project. This layer will handle all the interactions with the database and will be common for all of the pages.
The data layer that I am talking about is nothing but a group of classes that communicate with the database.
TheCPUWizard
December 12th, 2006, 08:52 AM
Shaja Ali gets it basically right, but I would like to expand on this.
Having connection specific information (as opposed to the connection itself) encapsulated is a notable goal. But it does have some serious issues especially when dealing with passwords. Realize than anytime a password is stored in user code, it is open to attack.
If you do want to centralize this information, then create a helper class which can use this information to create a connection, which can be created, used and disposed in a highly localized and robust fashion.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.