title: Duplicate values are inserted to table using sqlbulkcopy
body: <p>I have created gridview and inserting the values to datatable and then to database using sqlbulkcopy but when I add new row to the grid and click save button, previous rows and newly added row get insert to the DB. Anyone help me with how to insert only newly added row and update the previous rows when click on save button.</p>
  
  <p>here is my code</p>
  
  <pre><code>protected void save_Click(object sender, EventArgs e)
  {
      int rowIndex = 0;
      if (ViewState["CurrentTable"] != null)
      {
          DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
          //dtCurrentTable.Clear();
          if (dtCurrentTable.Rows.Count &gt; 0)
          {
              for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++)
              {
                  //extract the TextBox values
                  TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                  TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                  TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                  TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                  TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
                  TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
                  TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox7");
                  TextBox box8 = (TextBox)Gridview1.Rows[rowIndex].Cells[8].FindControl("TextBox8");
                  TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[9].FindControl("TextBox9");
                  TextBox box10 = (TextBox)Gridview1.Rows[rowIndex].Cells[10].FindControl("TextBox10");
                  TextBox box11 = (TextBox)Gridview1.Rows[rowIndex].Cells[11].FindControl("TextBox11");
                  TextBox box12 = (TextBox)Gridview1.Rows[rowIndex].Cells[12].FindControl("TextBox12");
                  TextBox box13 = (TextBox)Gridview1.Rows[rowIndex].Cells[13].FindControl("TextBox13");
  
                  //drCurrentRow = dtCurrentTable.NewRow();
                  //drCurrentRow["RowNumber"] = i + 1;
                  dtCurrentTable.Rows[i - 1]["stn_id"] = rowIndex + 1;
                  dtCurrentTable.Rows[i - 1]["unitname"] = box1.Text;
                  dtCurrentTable.Rows[i - 1]["weldmet"] = box2.Text;
                  dtCurrentTable.Rows[i - 1]["mfgparts"] = box3.Text;
                  dtCurrentTable.Rows[i - 1]["mylar"] = box4.Text;
                  dtCurrentTable.Rows[i - 1]["pin"] = box5.Text;
                  dtCurrentTable.Rows[i - 1]["stdpin"] = box6.Text;
                  dtCurrentTable.Rows[i - 1]["stdparts"] = box7.Text;
                  dtCurrentTable.Rows[i - 1]["ftweldmet"] = box8.Text;
                  dtCurrentTable.Rows[i - 1]["ftmfgparts"] = box9.Text;
                  dtCurrentTable.Rows[i - 1]["ftmylar"] = box10.Text;
                  dtCurrentTable.Rows[i - 1]["ftpin"] = box11.Text;
                  dtCurrentTable.Rows[i - 1]["ftstdpin"] = box12.Text;
                  dtCurrentTable.Rows[i - 1]["ftstdparts"] = box13.Text;
  
                  rowIndex++;
              } //for
              //dtCurrentTable.Rows.Add(drCurrentRow);
              ViewState["CurrentTable"] = dtCurrentTable;
  
              Gridview1.DataSource = dtCurrentTable;
              Gridview1.DataBind();
              // dtCurrentTable.Clear();
          }//dtcurrenttable
  
  
          string consString = ConfigurationManager.ConnectionStrings["gridconnection"].ConnectionString;
          using (SqlConnection connection = new SqlConnection(consString))
          {
              connection.Open();
              using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
              {
                  //foreach (DataColumn c in dt.Columns)
                  //   // bulkCopy.ColumnMappings.Add(c.Column1, c.Column2, c.Column3);
  
                  bulkCopy.DestinationTableName = "partexcel";
                  //bulkCopy.ColumnMappings.Add(Column1, c.Column2, c.Column3);
                  SqlBulkCopyColumnMapping stnid = new SqlBulkCopyColumnMapping("stn_id", "stn_id");
                  bulkCopy.ColumnMappings.Add(stnid);
                  SqlBulkCopyColumnMapping objunitname = new SqlBulkCopyColumnMapping("unitname", "unitname");
                  bulkCopy.ColumnMappings.Add(objunitname);
                  SqlBulkCopyColumnMapping objweldmet = new SqlBulkCopyColumnMapping("weldmet", "weldmet");
                  bulkCopy.ColumnMappings.Add(objweldmet);
                  SqlBulkCopyColumnMapping objmfgparts = new SqlBulkCopyColumnMapping("mfgparts", "mfgparts");
                  bulkCopy.ColumnMappings.Add(objmfgparts);
                  SqlBulkCopyColumnMapping objmylar = new SqlBulkCopyColumnMapping("mylar", "mylar");
                  bulkCopy.ColumnMappings.Add(objmylar);
                  SqlBulkCopyColumnMapping objpin = new SqlBulkCopyColumnMapping("pin", "pin");
                  bulkCopy.ColumnMappings.Add(objpin);
                  SqlBulkCopyColumnMapping objstdpin = new SqlBulkCopyColumnMapping("stdpin", "stdpin");
                  bulkCopy.ColumnMappings.Add(objstdpin);
                  SqlBulkCopyColumnMapping objstdparts = new SqlBulkCopyColumnMapping("stdparts", "stdparts");
                  bulkCopy.ColumnMappings.Add(objstdparts);
                  SqlBulkCopyColumnMapping objftweldmet = new SqlBulkCopyColumnMapping("ftweldmet", "ftweldmet");
                  bulkCopy.ColumnMappings.Add(objftweldmet);
                  SqlBulkCopyColumnMapping objftmfgparts = new SqlBulkCopyColumnMapping("ftmfgparts", "ftmfgparts");
                  bulkCopy.ColumnMappings.Add(objftmfgparts);
                  SqlBulkCopyColumnMapping objftmylar = new SqlBulkCopyColumnMapping("ftmylar", "ftmylar");
                  bulkCopy.ColumnMappings.Add(objftmylar);
                  SqlBulkCopyColumnMapping objftpin = new SqlBulkCopyColumnMapping("ftpin", "ftpin");
                  bulkCopy.ColumnMappings.Add(objftpin);
                  SqlBulkCopyColumnMapping objftstdpin = new SqlBulkCopyColumnMapping("ftstdpin", "ftstdpin");
                  bulkCopy.ColumnMappings.Add(objftstdpin);
                  SqlBulkCopyColumnMapping objftstdparts = new SqlBulkCopyColumnMapping("ftstdparts", "ftstdparts");
                  bulkCopy.ColumnMappings.Add(objftstdparts);
  
                  try
                  {
  
                      bulkCopy.WriteToServer(dtCurrentTable);
  
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                  }
              }
            }
          }
        }
  </code></pre>