Duplicate Values are inserted into database using DatagidView
foreach (DataGridViewRow dgRow in dataGridView1.Rows)
{
if (dgRow.Cells[0].Value != null)
{
string re1 = dgRow.Cells[0].Value.ToString();
string strQuery1 = "INSERT INTO unit_master(unit) VALUES('" + re1 + "')";
SqlCommand scmd1 = new SqlCommand(strQuery1,SqlConn);
scmd1.ExecuteNonQuery();
}
}
How to resolve it?
Anonymous User
30-Sep-2014Create Procedure
AS
@re1 Varchar(20)
BEGIN
if not exists (select * from unit_master d where d.unit = @re1)
INSERT INTO unit_master([unit ]) values(@re1)
END
Pravesh Singh
23-Dec-2013Write a Insert Procedure that will check duplicate insertion