title: C# Mysql Connection must be valid and open
body: <p>First of all: I got my code running without using oop. I declared all my variables inside the same class and opened/closed the connection right before and after passing the query to the db. That worked! Now with some new experiences I tried to split my code into different classes. Now it wont work anymore.</p>
  
  <p>It tells me "Connection must be valid and open". Enough text, here's my current code:</p>
  
  <hr>
  
  <p><strong>Services.cs</strong>  </p>
  
  <pre><code>public static MySqlConnection conn // Returns the connection itself
          {
              get
              {
                  MySqlConnection conn = new MySqlConnection(Services.ServerConnection);
                  return conn;
              }
          }
  
  public static string ServerConnection // Returns the connectin-string
          {
              get
              {    
                  return String.Format("Server={0};Port=XXXX;Database=xxx;Uid=xxx;password=xxXxxXxXxxXxxXX;", key);
              }
          }
  
  public static void DB_Select(string s, params List&lt;string&gt;[] lists)
          {
              try
              {
                  MySqlCommand cmd = conn.CreateCommand();
                  cmd.CommandType = CommandType.Text;
                  string command = s;
                  cmd.CommandText = command;
                  MySqlDataReader sqlreader = cmd.ExecuteReader();
                  while (sqlreader.Read())
                  {
                      if (sqlreader[0].ToString().Length &gt; 0)
                      {
                          for (int i = 0; i &lt; lists.Count(); i++)
                          {
                              lists[i].Add(sqlreader[i].ToString());
                          }
                      }
                      else
                      {
                          foreach (List&lt;string&gt; save in lists)
                          {
                              save.Add("/");
                          }
                      }
                  }
                  sqlreader.Close();
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Error while selecting data from database!\nDetails: " + ex);
              }
          }
  </code></pre>
  
  <p><strong>LoginForm.cs</strong></p>
  
  <pre><code>private void checkUser(string username, string password)
          {
          using (Services.conn)
                      {
                          Services.conn.Open();
                          Services.DB_Select("..a short select statement..");
                          Services.conn.Close();
                      }
  </code></pre>
  
  <hr>
  
  <p>I guess this is all we need. I have shortened my code to get a focus on the problem. </p>
  
  <p>I created Services.cs to get a global way to access the db from all forms without copy&amp;pasting the connection info. Now when I reach my LoginForm.cs it throws an error <strong>"Connection must be valid and open"</strong>. I've already debugged my code. It's all time closed. Even when passing conn.Open() it stays closed. Why?</p>
  
  <p><strong>Another try:</strong> I've also tried placing <code>conn.Open()</code> and <code>conn.Close()</code> inside <code>Services.DB_Select(..)</code> at the beginning and end. Same error here.</p>
  
  <p>I have to say: The code worked before and I've used the same connection-string. So the string itself is surely valid. </p>
  
  <p>I appreciate any help given here!</p>