开发者

Bulk Copy inserting each row twice

开发者 https://www.devze.com 2023-02-13 01:06 出处:网络
Now i got it to work but the bulk copy is inserting the rows twice, instead of once and i know the table has the correct rows because i can see it in the gridview correctly

Now i got it to work but the bulk copy is inserting the rows twice, instead of once

and i know the table has the correct rows because i can see it in the gridview correctly

I FIXED THIS PART

my program works fine when i run it in debug mode but not when i place it on the server, it doesnt run the stored procedure when i put it on my server.

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
                开发者_StackOverflow中文版    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 ((tasknamelist.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
            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

        cmd1.CommandText = "dbo.taskadding"
        cmd1.CommandType = CommandType.StoredProcedure
        cmd1.Connection = conn

        conn.Open()
        cmd1.BeginExecuteNonQuery()
        conn.Close()


Are you using the same DB server for both? (That is, is your connection string actually hard-coded, or did you modify it for simplicity's sake?)

If the server does not have mixed mode authentication turned on, you will not be able to connect with a username and password.

UPDATE:

As @santiagoIT mentioned, if you put exception handling around this and log the exception that is being generated, you will most likely see very quickly what is going wrong.

EDIT:

Per the request in the comments, here is how to implement exception handling with (rudimentary) logging:

Try
    'Your block of code
Catch ex As Exception
    System.IO.File.WriteAllText("MyLogFile.log", ex.ToString())
End Try

As mentioned by @santiagoIT, various logging frameworks exist to make logging more robust and stable (what I have done, for example, will throw an exception of its own if the log file is already open elsewhere). I would recommend something like this for a real solution, but what I have shown will help you debug the issue at hand.


I recommend that you install ELMAH. It is trivial to add but will give you HUGE benefits. It is an error logging framework. Once installed look up in the ELMAH error log and see the exception details. It is most certainly database related and the error log will should give you enough info to know what the problem is.

0

精彩评论

暂无评论...
验证码 换一张
取 消