Home > DeveloperSection > Forums > Duplicate Values are inserted into database using DatagidView
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    December-23-2013 2:48 AM

 C# C# 
Ratings:


 2 Reply(s)
 666  View(s)
Rate this:

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?



Pravesh Singh

Total Post:411

Points:2881
Posted on    December-23-2013 5:08 AM

Hi Jayden,

Write a Insert Procedure that will check duplicate insertion

Create 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


Pawan Shukla

Total Post:36

Points:260
Posted on    September-30-2014 1:49 AM

Create 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


Don't want to miss updates? Please click the below button!

Follow MindStick