title: Getting a ConnectionString from app.config
body: <p>I have a Web project which calls a library project (DataAccess) to retrieve some data from the database. I added an App.config file (Add -> New Item -> Application Configuration File) to the DataAccess project and added a connectionString section like this:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="local"
          connectionString="Data Source=.\sql2008;Initial Catalog=myDB;Integrated Security=True"
          providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>In the DataAccess project, I have the BuildConnection method:</p>
  
  <pre><code>internal static SqlConnection BuildConnection()
  {
      string connectionString = ConfigurationManager.ConnectionStrings["local"].ToString();
      return new SqlConnection(connectionString);
  }
  </code></pre>
  
  <p>When I call the method from the Web project, it throws a null exception complaining that the "local" connection string doesn't exist. After debugging it for a while I added the same connection string to the Web.config of the Web project, and now it works fine. The problem though is that I want to isolate the DataAccess project from the Web project, in other words, I want the DataAccess project to <strong>use its own app.config file</strong> no matter who calls it. Is this even possible? Any help would be appreciated.</p>