mind controll

mind controll

  • NA
  • 131
  • 60.6k

prevent insert of duplicated records sql

Jan 26 2014 4:40 PM

I need to prevent inserting duplicated records in a sql table and if exists show message says already exists


this is my button event :


private void button2_Click(object sender, EventArgs e)




{


conn.Open();


com.Connection = conn;



sql = "INSERT INTO lapusers([username],[fillingcode],[branch],[department],[agency])VALUES(@username,@fillingcode,@branch,@department,@agency)";




com.CommandText = sql;


com.Parameters.Clear();



com.Parameters.AddWithValue("@username", userlapbox.Text);


com.Parameters.AddWithValue("@fillingcode", userfilllapbox.Text);


com.Parameters.AddWithValue("@branch", comboBox2.Text);


com.Parameters.AddWithValue("@department", comboBox1.Text);


com.Parameters.AddWithValue("@agency", comboBox3.Text);




com.ExecuteNonQuery();



MessageBox.Show("Created Successfully ..");




conn.Close();


}




Answers (9)