bulk copy duplicating each row when it is added to database

asp.net sqlbulkcopy vb.net

Question

Why is my bulk copy duplicating each row, so in my database table the row shows twice.

Label1.Visible = True
    Dim tourid As New List(Of String)
    tourid.Add(TextBox1.Text)
    Dim tasktype As New List(Of String)
    Dim tourname1 As New List(Of String)
    Dim tasknamelist As New List(Of String)
    Dim dboxdates As New List(Of String)
    Dim dates As New List(Of String)
    Dim firstdates As New List(Of String)
    Dim agent As New List(Of String)
    Dim desc As New List(Of String)

    Dim checkitem As ListItem
    Dim departuredate As Date
    For Each checkitem In dboxes.Items
        If checkitem.Selected Then
            departuredate = checkitem.Text
            dboxdates.Add(departuredate)


            For Each row As GridViewRow In GridView1.Rows
                ' Selects the text from the TextBox


                Dim checkboxstatus As CheckBox = CType(row.FindControl("tasknamebox"), CheckBox)
                If checkboxstatus.Checked = True Then
                    tasknamelist.Add(checkboxstatus.Text)
                    Dim dates1 As TextBox = CType(row.FindControl("tdays"), TextBox)
                    Dim gracep As TextBox = CType(row.FindControl("tgrace"), TextBox)


                    Dim aftersubtraction As DateTime
                    Dim fromatafter As DateTime
                    aftersubtraction = departuredate.AddDays(-dates1.Text)
                    fromatafter = aftersubtraction.AddDays(-gracep.Text)
                    firstdates.Add(fromatafter.ToString("MM/dd/yyyy"))

                    While fromatafter.DayOfWeek = DayOfWeek.Saturday OrElse fromatafter.DayOfWeek = DayOfWeek.Sunday
                        fromatafter = fromatafter.AddDays(-2)
                    End While
                    dates.Add(fromatafter.ToString("MM/dd/yyyy"))




                    Dim txtdesc2 As TextBox = CType(row.FindControl("txtdesc"), TextBox)
                    desc.Add(txtdesc2.Text)

                    Dim tasktype1 As Label = CType(row.FindControl("tasktype"), Label)
                    Dim agentdlist As DropDownList = CType(row.FindControl("agentdlist"), DropDownList)


                    tasktype.Add(tasktype1.Text)

                    agent.Add(agentdlist.text)

                    Dim tourname As String
                    tourname = tname.Text
                    Dim sChars As String = " "
                    tourname1.Add(tourname.TrimEnd(sChars))

                End If
            Next
        End If

        If tasknamelist.Count > dboxdates.Count Then
            Do
                dboxdates.Add(checkitem.Text)

            Loop Until tasknamelist.Count = dboxdates.Count
        End If



        If tasknamelist.Count > tourid.Count Then
            Do
                tourid.Add(TextBox1.Text)

            Loop Until tasknamelist.Count = tourid.Count
        End If

    Next

    table.clear()


    For i As Integer = 0 To ((dates.Count) - 1)
        Dim row = table.NewRow()
        row("Tour") = tourid(i)
        row("TourName") = tourname1(i)
        row("Task") = tasknamelist(i)
        row("Departure") = dboxdates(i)
        row("Due Date") = dates(i)
        row("Task Type") = tasktype(i)
        row("Agent Name") = agent(i)
        row("Completed") = "NO"
        row("Description") = desc(i)
        row("Orig Due") = firstdates(i)
        table.Rows.Add(row)
    Next

    toptable.Visible = False
    bottom.Visible = True


    GridView2.DataSource = table
    GridView2.DataBind()


    Using bcp As SqlBulkCopy = New SqlBulkCopy(SqlDataSource2.ConnectionString)

        bcp.ColumnMappings.Add(0, 1)
        bcp.ColumnMappings.Add(1, 2)
        bcp.ColumnMappings.Add(2, 3)
        bcp.ColumnMappings.Add(3, 4)
        bcp.ColumnMappings.Add(4, 7)
        bcp.ColumnMappings.Add(5, 5)
        bcp.ColumnMappings.Add(6, 10)
        bcp.ColumnMappings.Add(7, 13)
        bcp.ColumnMappings.Add(8, 6)


        bcp.DestinationTableName = "dbo.stagingtasks"


        bcp.WriteToServer(table)

    End Using

Popular Answer

Your doing a table.Rows.Add(row) and a New SqlBulkCopy() They seem to be the same?



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why