title: how to read the connection string from App.config file by C#
body: <p>I'm using this code to read the connection string from my app.config file but it always return a null value. My App.config file is under my project. Both methods are resulting null values:</p>
  
  <pre><code>public SqlConnection getConnection()
  {
      try
      {
          // connectionString = ConfigurationManager.AppSettings["dbConn"];
  
          connectionString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
          connectionString = System.Configuration.ConfigurationManager.AppSettings["dbConn"];
          sqlConnection = new SqlConnection(connectionString);  
  
          sqlConnection = new SqlConnection(connectionString);
      }
      catch (Exception ex)
      {
  
      }
      return sqlConnection;
  }
  </code></pre>
  
  <p>This is my app.config file declaration:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
   &lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="dbConn" providerName="System.Data.SqlClient"
            connectionString="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User  Id=sa;Password=123" /&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>