title: matching the data entered in the combobox to the database
body: <p>Here I want to verify the data entered by the user by using the leave event function so what I want to do here is to match the data entered by the user in the database. I did it by using this code here but I'm not sure if I'm doing it right. I tried to run it but it doesn't work.</p>
  
  <pre><code>private void cboBranch_Leave(object sender, EventArgs e)
  {
  
      if (cboBranch.Text.Length &gt; 0)
      {
          try
          {
              using (MySqlConnection con = new MySqlConnection(serverstring))
              {
                  string query = "SELECT * FROM tblBranches WHERE branch_name=@branch";
  
                  con.Open();
                  using (MySqlCommand cmd = new MySqlCommand(query, con))
                  {
                      using (MySqlDataReader dr = cmd.ExecuteReader())
                      {
                          cmd.Parameters.Add("@branch", MySqlDbType.VarChar, 30).Value = cboBranch.Text;
  
                          int count = 0;
                          bool flag = false;
  
                          while (dr.Read())
                          {
                              count++;
                              if (count == 1)
                              {
                                  flag = true;
                              }
                          }
  
                          if (flag == false)
                          {
                              MessageBox.Show("The Branch name you entered does not match.", "AFICIONADO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
              cboBranch.Select();
                          }
                      }
  
                  }
              } 
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
  
      }
  }
  </code></pre>