title: C#.net Windows Application - Input data check before insert to Database
body: <p>I am making a booking system.</p>
  
  <p>I can't figure out the validation algorithm for a series of data before it insert to DB.
  The primary key will be the booking ID which is automatic generate by the system.</p>
  
  <p>I need to validate the bdate, btime and sname. (bdate=booking time, btime=booking time, and sname=staff name)</p>
  
  <p>In case of the bdate, btime and sname is same as what the client input. the system will alert its duplicate as the staff already have booking on the same date and time.</p>
  
  <p>Please find my insert query at below and appreciated you can point me to the right way.</p>
  
  <pre><code> private void btn_save_Click(object sender, EventArgs e)
      {
          OleDbCommand cmd = new OleDbCommand();
          cmd.CommandType = CommandType.Text;
  
          //query for duplicate
          cmd.CommandText = "select count(*) from Booking where sname = @newName and bdate = @newDate and btime = @newTime";
         // cmd.Parameters.Add("@newName", OleDbType.VarChar).Value = txt_cname.Text;
          //cmd.Parameters.Add("@newDate", OleDbType.DBDate).Value = dtp_bdate.Value.Date;
         // cmd.Parameters.Add("@newTime", OleDbType.VarChar).Value = dtp_btime.Value.ToString("hh:mm tt");
  
          cmd.CommandText = "insert into Booking(cname, bdate, btime, ccontact, sname) Values('" + txt_cname.Text + "','" + dtp_bdate.Value.Date + "','" + dtp_btime.Value.ToString("hh:mm tt") + "','" + txt_ccontact.Text + "','" + txt_sname.Text + "')";
          cmd.Connection = myCon;
          myCon.Open();
  
          int recordCount = Convert.ToInt32(cmd.ExecuteScalar());
  
          myCon.Close();
  
          if (recordCount&gt;0)
  
          {
              // handle duplicates
              MessageBox.Show("Duplicated", "My Application",
              MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
          }
  
         // cmd.Connection = myCon;
          //myCon.Open();
         //cmd.ExecuteNonQuery();
          //myCon.Close();
          //MessageBox.Show(dtp_bdate.Value.ToString());
          //MessageBox.Show("Booking completed", "My Application",
         // MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
      }
  </code></pre>