Global variables where do they go?
-
Hi, I'm new to ASP.NET and I'm trying to create an (C#) application that uses global variables. I have a database connection string and I want to be able to use it in many of my pages. How can I do this? I created an object to do this, but I don't want to be forced to create that object on every page which uses the database. Thanks :)
-
Hi, I'm new to ASP.NET and I'm trying to create an (C#) application that uses global variables. I have a database connection string and I want to be able to use it in many of my pages. How can I do this? I created an object to do this, but I don't want to be forced to create that object on every page which uses the database. Thanks :)
You can use Session to store global variables but thats not goood idea for connection string because if you want to change it you need to recompilation so it is better to store it in web.config file located in your root directory.Add this key at the end of it before closing Configuration tag: Whenever you want to get this value you can get it like this: string str = ConfigurationSettings.AppSettings["Connection_Setting"];
-
You can use Session to store global variables but thats not goood idea for connection string because if you want to change it you need to recompilation so it is better to store it in web.config file located in your root directory.Add this key at the end of it before closing Configuration tag: Whenever you want to get this value you can get it like this: string str = ConfigurationSettings.AppSettings["Connection_Setting"];
-
The Object Session is global.asax's OnStartApplication??? I remember a example into the SDK documentation. ---- hxxbin
Mazdak's suggestion would probably be more efficient.
// Steve McLenithan
Cluelessnes:
There are no stupid questions, but there are a lot of inquisitive idiots. -
Hi, I'm new to ASP.NET and I'm trying to create an (C#) application that uses global variables. I have a database connection string and I want to be able to use it in many of my pages. How can I do this? I created an object to do this, but I don't want to be forced to create that object on every page which uses the database. Thanks :)
I'd go with Mazdax on putting the connection string in the AppSetting section of the web.config file. However, what I'd also do is set up a Singleton class (that is a class where only one instance ever exists) to manage your global configuration stuff. e.g.
class AppConfiguration
{
public static string ConnectionString
{
get
{
return System.Configuration.ConfigurationSettings.AppSettings["DbConnectionString"];
}
}public static SqlConnection { get { return new SqlConnection(ConnectionString); } }
}
This will also create your connection object for you too - Which produces much cleaner code. --Colin Mackay--
-
The Object Session is global.asax's OnStartApplication??? I remember a example into the SDK documentation. ---- hxxbin