title: Calling Connection string from App setings app.config file to C# code
body: <p>I'm trying to call a connection string from the app.config file to C# code. This is the app.config code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
    &lt;configuration&gt;
   &lt;appSettings&gt;
  
  &lt;add key="connectstring" value="Data Source=server111;Initial Catalog=database1;        Integrated Security=False;Persist Security Info=False; User ID=username;Password=password;Encrypt=True; TrustServerCertificate=True; MultipleActiveResultSets=True"/&gt;
  
   &lt;/appSettings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>This is the C# code: </p>
  
  <pre><code>  private SqlConnection connStudent;  
    connStudent = new SqlConnection(ConfigurationManager.AppSettings["connectstring"].ToString());
    connStudent.Open();
  </code></pre>
  
  <p>The code should be right, but I'm getting a Null Reference Exception, and while debugging the program, connStudent is always null and does not get the connection string .
  The error is "Object reference not set to an instance of an object".</p>