DUPLICATE VALUES ARE INSERTED INTO DATABASE USING DATAGIDVIEW

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
C# 
C#
 1039  View(s)
Ratings:
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?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Duplicate Values are inserted into database using DatagidView

    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

      Modified On Apr-05-2018 06:24:16 AM
  1. Pawan Shukla

    Post:36

    Points:260
    Re: Duplicate Values are inserted into database using DatagidView

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!