title: Winforms connection strings from App.config
body: <p>I have a Winforms app that I am developing in C# that will serve as a frontend for a SQL Server 2005 database. I rolled the executable out to a test machine and ran it. It worked perfectly fine on the test machine up until the last round of changes that I made. However, now on the test machine, it throws the following exception immediately upon opening:</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
         at PSRD_Specs_Database_Administrat.mainMenu.mainMenu_Load(Object sender, EventArgs e)
         at System.Windows.Forms.Form.OnLoad(EventArgs e)
         at System.Windows.Forms.Form.OnCreateControl()
         at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
         at System.Windows.Forms.Control.CreateControl()
         at System.Windows.Forms.Control.WmShowWindow(Message&amp; m)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
         at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)
         at System.Windows.Forms.Form.WmShowWindow(Message&amp; m)
         at System.Windows.Forms.Form.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  </code></pre>
  
  <p>The only thing that I changed in this version that pertains to mainMenu_Load is the way that the connection string to the database is called. Previously, I had set a string with the connection string on every form that I needed to call it from, like:</p>
  
  <pre><code>string conString = "Data Source = SHAREPOINT;Trusted_Connection = yes;" +
                     "database = CustomerDatabase;connection timeout = 15";
  </code></pre>
  
  <p>As my app grew and I added forms to it, I decided to add an App.config to the project. I defined the connection string in it:</p>
  
  <pre><code>&lt;connectionStrings&gt;
    &lt;add name="conString"
     providerName="System.Data.SqlClient"
     connectionString="Data Source = SHAREPOINT;Trusted_Connection = yes;database = CustomerDatabase;connection timeout = 15" /&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>I then created a static string that would return the conString: </p>
  
  <pre><code>public static string GetConnectionString(string conName)
  {
      string strReturn = string.Empty;
      if (!(string.IsNullOrEmpty(conName)))
      {
          strReturn = ConfigurationManager.ConnectionStrings[conName].ConnectionString;
      }
      else
      {
          strReturn = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
      }
      return strReturn;
  }
  </code></pre>
  
  <p>I removed the conString variable and now call the connection string like so:</p>
  
  <pre><code>PublicMethods.GetConnectionString("conString").ToString()
  </code></pre>
  
  <p>It appears that this is giving me the error. I changed these instances to directly call the connection string from App.config without using GetConnectionString. For instance, in a SQLConnection:</p>
  
  <pre><code>using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
  </code></pre>
  
  <p>This also threw the exception. However, when I went back to using the conString variable on each form, I had no issues. What I don't understand is why all three methods work fine on my development machine, while using the App.config directly or via the static string I created throw exceptions.</p>