I've got a datagridview populated from a MySQL db. I'm looping through it to validate the info and, when info is validated, adding records to a datatable.
The problem i've got is that my current code only adds the first record. I expected it to add a record, move on and then add another record and move on etc.
Any ideas?
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
The problem is that you put the add statements inside the for each loop. So in the second iteration you're trying to add an already existing column named Product Ordered
to the DataTable
. That's why an exception is thrown and you reach the catch block. The solution is to put the add statements before the for each loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.