title: How to validate winforms field for uniqueness
body: <p>I have code that checks for unique values when the user updates an ID field, but I am so new I am wondering if there is a better way. </p>
  
  <pre><code>private void tbPrinterID_Validating(object sender, CancelEventArgs e)
  {
      using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.LazerMaintenance_Conn))
      {
          try
          {
              string query = "SELECT COUNT(*) as Count FROM Printers WHERE PrinterID = '" + tbPrinterID.Text + "'";
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              DataTable dt = new DataTable();
              da.Fill(dt);
              if ((Int32)dt.Rows[0]["Count"] &gt; 0)
              {
                  MessageBox.Show("There is already a printer with ID = " + tbPrinterID.Text);
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error occured! : " + ex);
          }
      }
  }
  </code></pre>