Israel

Israel

  • 698
  • 1.3k
  • 217.7k

Specific condition against repeated datas...

Jul 9 2021 2:29 PM

Hi,

I wrote these codes to save datas (text and image). I need to reject the repeated name (for example: This name already exist). How can I do?

try
{
    byte[] img1 = null;
    FileStream fs1 = new FileStream(imgLoc1, FileMode.Open, FileAccess.Read);
    BinaryReader br1 = new BinaryReader(fs1);
    img1 = br1.ReadBytes((int)fs1.Length);
     string sql = "INSERT INTO intelflex1(ID, name,  age, imagem1)Values(" + txtID.Text + ",'" + txtName.Text  + "','" + txtAge + "','" + "',@img1)";
    if (conn.State != ConnectionState.Open)
        conn.Open();
    comm = new SqlCommand(sql, conn);
    comm.Parameters.Add(new SqlParameter("@img1", img1));
    int x = comm.ExecuteNonQuery();
    conn.Close();
    MessageBox.Show(x.ToString() + "saved successfully!");
}
catch (Exception ex)
{
    conn.Close();
    MessageBox.Show(ex.Message);
}

 


Answers (1)