Back with ASP.net 1.1, if you set a connection string in your Web.config file, you could set it with something like this:
<appSettings> <add key="ConnectionStringName" value="ConnectionStringData" /> </appSettings>
and get access to the string in your code with something like this:
string connString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionStringName"];
With ASP.net 2.0, while you can still use appsettings if you so desire, there are now subsections allowed in the Web.config, one of which is set aside for database connection strings. Here is how to use it:
<connectionStrings> <add name="ConnectionStringName" connectionString="ConnectionStringDetails"/> </connectionStrings>
The good news with this is that the connection string within your Web.config file is no longer just some string (that while you may know it is a connection string, your application has no intrinsic knowledge of this). Your website can now access this string in a number of different ways, in general making your life easier (especially when using the Website Configuration screens, or any of the built-in data access controls).
All of this is old news. However, while the process for retrieving a connection string in 2.0 is well-documented when you are doing it from within the setup wizard of a DataAccess control, I had some trouble figuring out how to retrieve it solely within the code. Eventually (with some help from Google) I was able to find the exact syntax, and I am passing it on here so that it might help someone else facing the same problem.
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;