title: Add connectionstring in app.config
body: <p>I have a class library in C# has has several classes and a app.config. I have put ConnectionString in app.config as follows:</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="TRN_DB"
           connectionString="Database=TRN;Server=NDTSSN;User ID=ln_users2;Password=%4nu$r!;Connection Timeout=30;pooling=false;" 
           providerName="System.Data.SqlClient"
      /&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>I try to open connection string in one of the classes in my project as follows:</p>
  
  <pre><code>     string connectionString = ConfigurationManager.ConnectionStrings["TRN_DB"].ConnectionString.ToString();
         SqlConnection con = new SqlConnection(connectionString);
         con.Open();
  </code></pre>
  
  <p>But it gave me an error as:</p>
  
  <blockquote>
    <p>{"Object reference not set to an instance of an object."}</p>
  </blockquote>
  
  <p>The connection string is correct when I use same way in web project inside the web.config, Does anybody know how how I can access it in app.config? I already use it this way too:</p>
  
  <pre><code> string connectionString = System.Configuration.ConfigurationSettings.AppSettings["TRN_DB"];
  </code></pre>
  
  <p>this one give me this error:</p>
  
  <blockquote>
    <p>This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.Appsettings</p>
  </blockquote>
  
  <p>To make it clear, I don't have any other project in my solution, as I want my class library to work independently. I am not able to app web project and web.config to that. So I either should add connection string to app.config and access it Or add a web.config to my class library project. That I am not sure which one is doable.</p>