Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在datagridview中循环时向datatable添加行_Vb.net_Loops_Datagridview_Datatable - Fatal编程技术网

Vb.net 在datagridview中循环时向datatable添加行

Vb.net 在datagridview中循环时向datatable添加行,vb.net,loops,datagridview,datatable,Vb.net,Loops,Datagridview,Datatable,我已经从MySQL数据库中填充了datagridview。 我通过它来验证信息,并且在验证信息时,向数据表添加记录 我遇到的问题是,我当前的代码只添加了第一条记录。 我希望它添加一个记录,继续,然后添加另一个记录,继续等等 有什么想法吗 Private Sub ProcessorWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles ProcessorWorker.DoWork

我已经从MySQL数据库中填充了datagridview。 我通过它来验证信息,并且在验证信息时,向数据表添加记录

我遇到的问题是,我当前的代码只添加了第一条记录。 我希望它添加一个记录,继续,然后添加另一个记录,继续等等

有什么想法吗

    Private Sub ProcessorWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles ProcessorWorker.DoWork
    Console.WriteLine("ProcessorWorker Invoked...")
    Try
        Dim dt As New DataTable

        Dim entryid As String = " "
        For Each row As DataGridViewRow In orderslist.Rows

            If row.Cells("member_id").Value.ToString IsNot Nothing Then
                entryid = row.Cells("entry_id").Value.ToString
                If entryid <> "" Then
                Dim memberid As String = row.Cells("member_id").Value.ToString

                    Dim cn1 As New MySqlConnection
                    cn1.ConnectionString = ###myconnectionstring###
                    Dim vm_components As New MySqlDataAdapter("Select * FROM website_orders WHERE order_id= '" & entryid & "'", cn1)
                    Dim vm_components_table As New DataTable
                    vm_components.Fill(vm_components_table)
                    Dim row1 As DataRow
                    row1 = vm_components_table.Select("order_id = '" & entryid & "'").FirstOrDefault()
                    If Not row1 Is Nothing Then
                        If row1.Item("cart_id") IsNot Nothing Then web_cartid = row1.Item("cart_id").ToString
                        If row1.Item("email") IsNot Nothing Then web_email = row1.Item("email").ToString
                        If row1.Item("member_id") IsNot Nothing Then web_memberid = row1.Item("member_id").ToString
                        If row1.Item("firstname") IsNot Nothing Then web_firstname = row1.Item("firstname").ToString
                        If row1.Item("lastname") IsNot Nothing Then web_lastname = row1.Item("lastname").ToString
                        If row1.Item("company") IsNot Nothing Then web_company = row1.Item("company").ToString
                        If row1.Item("address1") IsNot Nothing Then web_address1 = row1.Item("address1").ToString
                        If row1.Item("address2") IsNot Nothing Then web_address2 = row1.Item("address2").ToString
                        If row1.Item("city") IsNot Nothing Then web_city = row1.Item("city").ToString
                        If row1.Item("postcode") IsNot Nothing Then web_postcode = row1.Item("postcode").ToString
                    End If

                    dt.Columns.Add("Product Ordered", Type.GetType("System.String"))
                    dt.Columns.Add("Operating System", Type.GetType("System.String"))
                    dt.Columns.Add("Company", Type.GetType("System.String"))
                    dt.Columns.Add("Customer", Type.GetType("System.String"))
                    dt.Columns.Add("Email Address", Type.GetType("System.String"))

                    WebOrders.DataSource = dt
                    Dim dr As DataRow
                    dr = dt.NewRow
                    dr("Product Ordered") = web_servertype
                    dr("Operating System") = web_os
                    dr("Company") = "WORLD"
                    dr("Customer") = web_firstname & " " & web_lastname
                    dr("Email Address") = web_email
                    dt.Rows.Add(dr)
                    WebOrders.AllowUserToAddRows = False
                End If
            End If
        Next

    Catch ex As Exception

    End Try

End Sub

问题是您将add语句放在for each循环中。因此,在第二次迭代中,您试图将一个名为Product ORDERS的现有列添加到数据表中。这就是为什么会抛出异常,然后到达catch块。解决方案是将add语句放在for each循环之前。

我不知道为什么和/或是谁删除了我的评论,但公认的答案应该解决以下问题:1吞咽异常是一种不好的做法。BackgroundWorker有自己的错误处理机制。3交叉线程异常的可能性。4始终处理IDisposable对象。5始终使用准备好的/参数化的SQL。