title: Get ConnectionString from app.config
body: <p>I'm pretty sure there's some quick and easy error in this code but somehow I've spent the last 2 hours with this and couldn't solve it.</p>
  
  <p><code>App.config</code>:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="BO"
          connectionString="Data Source=MyServer;Initial Catalog=BO;User ID=WebUser;Password=MyPasswd"
          providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><code>Class.cs</code>:</p>
  
  <pre><code>string connectionString = getNewConnection();
  using (SqlConnection conn = new SqlConnection(connectionString)) { code }
  </code></pre>
  
  <p>Method.</p>
  
  <pre><code>public static string getNewConnection()
  {
     return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
   }
  </code></pre>
  
  <p>Error: </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>on the line : </p>
  
  <pre><code>return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
  </code></pre>
  
  <p>EDIT:</p>
  
  <p><img src="https://i.stack.imgur.com/gnMDx.jpg" alt="Error image, the Spanish sentence means: Object reference not set to an instance of an object"></p>