Mfwamba Tshimanga

Mfwamba Tshimanga

  • NA
  • 155
  • 31.7k

Just don't repeat the same record please!

Jul 6 2014 7:55 PM
Hi!

Please I am trying to don't allow repetition of records. But I cannot resolv it.
It says it's saved but he didnt (the same datas or anothers when I open my sql server table I didnt find the record that was saved as he said).

conn = new SqlConnection(connstr);
                comm = new SqlCommand();
                conn.Open();

                SqlParameter data = new SqlParameter("@data", SqlDbType.NChar);
                SqlParameter classe = new SqlParameter("@classe", SqlDbType.NChar);
                
                comm.Parameters.Add(data);
                comm.Parameters.Add(classe);
                       
                data.Value = DateTime.Today;
                classe.Value = codigoBarraStocks.Text;
                
                comm.Connection = conn;
        
                comm.CommandText = "select count(*) from stocks where classe = @classe";
                int count = (int)comm.ExecuteScalar();
                if (count == 0)
                try
                {
                    comm.ExecuteNonQuery();
                    MessageBox.Show("Saved succefully");
                    
                }
                catch (Exception)
                {
                    MessageBox.Show("Not saved");
                }
                finally
                {
                    conn.Close();
                }

Answers (1)