Geeks With Blogs
Notes Etc...

ASP.Net 2.0 allows storing ConnectionString elements within the ConnectionStrings section in web.config. The web.config section containing ConnectionStrings can be encrypted for security.

ConnectionStrings can be referred via Expressions which are resolved by the ASP.net parser at run-time. When setting the ConnectionString for DataSources, Expressions are typically used to fetch the corresponding ConnectionString element

The ConnectionStrings stored in the web.config's ConnectionStrings section can also be retrieved in code at run-time by using the Configuration object. The correct web.config file needs to be specified in the call to the OpenWebConfiguration() method. The following code snippet shows how to retrieve the ConnectionString element in code, MyWebSite is the name of the website and MyWebDBConnectionString is the name of the ConnectionString element stored in the web.config file.

//Retrieve ConnectionString from the web.config ConnectionStrings section

Dim oConfig As 
System.Configuration.Configuration
Dim strConnectionString 
as String

oConfig System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSite"
)

If (oConfig.ConnectionStrings.ConnectionStrings.Count > 
Then
    strConnectionString oConfig.ConnectionStrings.ConnectionStrings("MyWebDBConnectionString"
).ConnectionString
Else
    Throw New 
ApplicationException("Could not load the database"
)
End If

The above code would be useful if you wanted to retrieve a Connectionstring from web.config for a SQLConnection created manually or if you wanted to save the Connectionstring in the Application variables.

Resources for Encrypting Configuration Sections
Encrypted Configuration Sections
How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA
How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI

Posted on Thursday, November 17, 2005 5:09 PM .Net 2.0 | Back to top


Comments on this post: ASP.Net 2.0: Retrieve ConnectionString values in code

# re: ASP.Net 2.0: Retrieve ConnectionString values in code
Requesting Gravatar...
didn't this use to be alot simpler in asp.net 1.1??

but thanks you saved me today.
Left by matt on Apr 08, 2006 7:03 PM

# re: ASP.Net 2.0: Retrieve ConnectionString values in code
Requesting Gravatar...
Thanx a lot. I was doing this without specifying the webconfig file. I had there "Nothing" intead of "WebSiteName"...You saved the day!!!
Left by Lebza on Aug 03, 2006 8:35 AM

# re: ASP.Net 2.0: Retrieve ConnectionString values in code
Requesting Gravatar...
Luckily it's a lot simpler than that:
dim str as string=ConfigurationManager.ConnectionStrings("yoursqldb").ConnectionString

Brgds Henri
Left by Henri on Jan 22, 2007 12:36 PM

# re: ASP.Net 2.0: Retrieve ConnectionString values in code
Requesting Gravatar...
Thank you Henri! That's exactly what I needed!
Left by Jeremy on Mar 22, 2007 1:59 PM

# re: ASP.Net 2.0: Retrieve ConnectionString values in code
Requesting Gravatar...
Thank you Henri! this is exactly what I was looking for!
Left by Jacob Mathew on Dec 04, 2007 12:25 AM

Your comment:
 (will show your gravatar)


Copyright © Notes Etc... | Powered by: GeeksWithBlogs.net