Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



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

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:

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

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

[code:c#]
string strConnString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
[/code]

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.

Currently rated 4.3 by 16 people

  • Currently 4.3125/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: General
Posted by crpietschmann on Wednesday, December 28, 2005 11:18 PM
Permalink | Comments (11) | Post RSSRSS comment feed

Related posts

Comments

Dirk Knoblett

Monday, January 02, 2006 6:09 AM

Dirk Knoblett

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

Monday, January 02, 2006 6:58 AM

Dirk Knoblett

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

Wednesday, March 01, 2006 3:44 PM

Chris Shepherd

Thanks - just what I was looking for.

Jeff Bandy

Wednesday, May 17, 2006 9:30 PM

Jeff Bandy

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

Tuesday, August 29, 2006 5:24 PM

Peter King

Excellent, thanks!

=[`]

Thursday, September 28, 2006 1:13 PM

=[`]

Thanks for posting this.
I knew it was something simple, but I couldn't put my finger on it!

Chris

Thursday, September 28, 2006 8:12 PM

Chris

Thanks, just what I wanted.

John

Wednesday, November 22, 2006 3:21 AM

John

Thanks for the great tip - I have been looking for this for a long time!

sim

Wednesday, December 06, 2006 12:31 AM

sim

Excellent!! It helped me a lot. To make somebody come out of frustration is a wonderful thing. Keep up this good work

Andrei

Friday, January 05, 2007 4:28 PM

Andrei

Brilliant, I did as it says at the top of this page and I have a connection to my database.
Thank you
Andrei

Karthick

Saturday, January 13, 2007 11:23 AM

Karthick

thank u so much,its realy usefull for new learning people

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann