8
Answers

prevent insert of duplicated records sql

mind controll

mind controll

9y
10k
1

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 (8)