ASP.NET: How to get a specific ConnectionString from the Web.Config by name

28. December 2005

You have your ConnectionString for your ASP.NET web app stored in the Web.Config file. Now how exactly how do you get that ConnectionString out of there from within your code?

Sample Web.Config section with a ConnectionString:


<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Data Source=myDBServer;database=myDB;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>

Now lets get the ConnectionString from the Web.Config file with only one line of code:


string strConnString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

VB:
Dim strConnString As String = ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString

Now isn't that simple? I'm posting this because I did a search and didn't find an example of how to do this. I had to poke around a little and discover this on my own. I hope this helps someone avoid some frustration.

asp.net ,



Comments

Dirk Knoblett
Dirk Knoblett
1/4/2006 4:09:00 AM #
I already attempted to use code like you outline with a MS Access database and was receiving an error when the database connection opened.

My web.config file connection string:
<connectionStrings>
    <remove name="AccessFileName"/>
    <add name="AccessFileName" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=~/App_Data/ASPNetDB.mdb" providerName="System.Data.OleDb"/>
  </connectionStrings>

My VB http://www.microsoft.com/net/" rel="nofollow">http://www.microsoft.com/net/" target="_blank">.NET connection string:
Dim connString As String = ConfigurationManager.ConnectionStrings("AccessFileName").ConnectionString

Exception Details: System.Data.OleDb.OleDbException: 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\~\App_Data\ASPNetDB.mdb' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

I corrected this error by not referencing the web.config file, changing the VB http://www.microsoft.com/net/" rel="nofollow">http://www.microsoft.com/net/" target="_blank">.NET connection string to:
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("~/App_Data/ASPNetDB.mdb")

Was I doing something incorrect?


Dirk Knoblett
Dirk Knoblett
1/4/2006 4:58:00 AM #
Correction to my post. I copied the wrong web.config snippet, here is the one I'm using.
<connectionStrings>
    <remove name="AccessFileName"/>
    <add name="AccessFileName" connectionString="~/App_Data/ASPNetDB.mdb" providerName="System.Data.OleDb"/>
  </connectionStrings>
Chris Shepherd
Chris Shepherd
3/3/2006 1:44:00 PM #
Thanks - just what I was looking for.
Jeff Bandy
Jeff Bandy
5/19/2006 7:30:00 PM #
I just did what you were talking about, searching for an example, and found your code to speed me along.  Thanks a ton!
Peter King
Peter King
8/31/2006 3:24:00 PM #
Excellent, thanks!
=[`]
=[`]
9/30/2006 11:13:00 AM #
Thanks for posting this.
I knew it was something simple, but I couldn't put my finger on it!
Chris
Chris
9/30/2006 6:12:00 PM #
Thanks, just what I wanted.
John
John
11/24/2006 1:21:00 AM #
Thanks for the great tip - I have been looking for this for a long time!
sim
sim
12/7/2006 10:31:00 PM #
Excellent!! It helped me a lot. To make somebody come out of frustration is a wonderful thing. Keep up this good work
Andrei
Andrei
1/7/2007 2:28:00 PM #
Brilliant, I did as it says at the top of this page and I have a connection to my database.
Thank you
Andrei
Karthick
Karthick
1/15/2007 9:23:00 AM #
thank u so much,its realy usefull for new learning people
Comments are closed