title: C# error "The connection was not closed. The connection's current state is open"
body: <p>I have this code and everything is fine except that when I click the save button, an error is occurred on the <code>connection.Open();</code> under <code>idpicture = brs.ReadBytes((int)Stream.Length);</code>.</p>
  
  <p>I am a beginner in C# so please bear with me.</p>
  
  <pre><code>private void button6_Click(object sender, EventArgs e)
          {
              if (File.Exists(op.FileName))
              {
                  object sel = comboBoxcourse.SelectedValue;
  
                  byte[] idpicture = null;
                  FileStream Stream = new FileStream(imgLocation, FileMode.Open, FileAccess.Read);
                  BinaryReader brs = new BinaryReader(Stream);
                  idpicture = brs.ReadBytes((int)Stream.Length);
                  connection.Open();
  
                  if (textBoxidyear.Text == "" || textBoxidnumber.Text == "" || textBoxfirstname.Text == "" || textBoxmiddlename.Text == "" ||
                              textBoxlastname.Text == "" || comboBoxcourse.Text == "")
                  {
                      MessageBox.Show("Please complete the form!");
                  }
                  else
                  {
                      cmd = new SqlCommand("SELECT * FROM Sstudent WHERE idnumber ='" + textBoxidnumber.Text + "' ", connection);
                      SqlDataAdapter da = new SqlDataAdapter(cmd);
                      da.Fill(ds);
                      int i = ds.Tables[0].Rows.Count;
  
                      if (i &gt; 0)
                      {
                          MessageBox.Show("Id Number " + textBoxidnumber.Text + " already exists!");
                      }
                      else
                      {
                          try
                          {
                              //connection.Open();
                              string query = @"INSERT INTO Sstudent(idnumber,idyear,idpicture,firstName,middleName,lastName,gender,course) 
                                          Values(@idnumber,@idyear,@idpicture,@firstName,@middleName,@lastName,@gender,@course)";
  
                              cmd = new SqlCommand(query, connection);
                              using (SqlConnection xcon = new SqlConnection(@"Server=GRACELIEZEL\SQLEXPRESS;Database=Final_Project;Integrated Security=SSPI;"))
                              {
                                  using (SqlCommand xcom = new SqlCommand(query, xcon))
                                  {
                                      xcom.CommandType = CommandType.Text;
  
                                      cmd.Parameters.Add(new SqlParameter("@idnumber", textBoxidnumber.Text));
                                      cmd.Parameters.Add(new SqlParameter("@idyear", textBoxidyear.Text));
                                      cmd.Parameters.Add(new SqlParameter("@idpicture", idpicture));
                                      cmd.Parameters.Add(new SqlParameter("@firstName", textBoxfirstname.Text));
                                      cmd.Parameters.Add(new SqlParameter("@middleName", textBoxmiddlename.Text));
                                      cmd.Parameters.Add(new SqlParameter("@lastName", textBoxlastname.Text));
                                      cmd.Parameters.Add(new SqlParameter("@gender", GetGender()));
                                      cmd.Parameters.Add(new SqlParameter("@course", comboBoxcourse.GetItemText(comboBoxcourse.SelectedItem)));
  
                                      int N = cmd.ExecuteNonQuery();
                                      connection.Close();
  
                                      MessageBox.Show(" Saved Successfully...!");
                                      AddStudent p = new AddStudent();
                                      p.Show();
                                      this.Hide();
                                  }
                              }
  
                          }
                          catch (Exception)
                          {
                              throw;
                          }
                          finally
                          {
                              connection.Close();
                          }
                      }
                  }
              }
              else
              {
              MessageBox.Show("Please choose an image.");
              }
          }
  </code></pre>