title: Why do I get a "connection is already open" error during a MySQL update statement?
body: <p>I am doing a college attendance project with Winforms, MySQL, and C#.</p>
  
  <p>In that I want to update "Total Absent" and "Total Present" in one table. So I wrote code for that: </p>
  
  <pre><code>int p = 0;
  int a = 0;
  string sno = "";
  
  MySqlDataReader Reader;
  command.CommandText = "select * from attendance_monthly_rpt";
  connection.Open();
  Reader = command.ExecuteReader();           
  
  while (Reader.Read())
  {
      p = 0;
      a = 0;
      sno = Reader[1].ToString();
      for (int k = 3; k &lt;= 33; k++)
      {
          if ((Reader[k].ToString() == "P") || (Reader[k].ToString() =="OD"))
          {
              p += 1;
          }
          else if (Reader[k].ToString() == "Ab")
          {
              a += 1;
          }
      }
  }
  connection.Close();
  </code></pre>
  
  <p>Now I get "sno", "a", "p" values for that. But I want to update this record in the MySQL table. So I added an <code>UPDATE</code> statement:</p>
  
  <pre><code>while (Reader.Read())
  {
      p = 0;
      a = 0;
      sno = Reader[1].ToString();
      for (int k = 3; k &lt;= 33; k++)
      {
          if ((Reader[k].ToString() == "P") || (Reader[k].ToString() =="OD"))
          {
              p += 1;
          }
          else if (Reader[k].ToString() == "Ab")
          {
              a += 1;
          }
      }           
      command.CommandText = "update attendance_monthly_rpt set tot_persent = " +
                            p + ", tot_absent = " + a + " where student_no = '" +
                            sno + "'";
      connection.Open();  // the connection is already open
      command.ExecuteNonQuery();
      connection.Close();
  }
  connection.Close()
  </code></pre>
  
  <p>This throws the error <em>"The connection is already open"</em>. If I place the update statement after the loop, I get the last record only.</p>
  
  <p>Please help me to update my "Tot absent", "Tot present" for each "Student".</p>