Question How to insert record from gridview to database in background worker w/ progress bar?

trialer

Well-known member
Joined
Oct 4, 2010
Messages
64
Programming Experience
1-3
VB.NET:
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
      With DataGridView1
            For i As Long = 1 To 100
                BackgroundWorker1.ReportProgress(i)
                For Each row As DataGridViewRow In .Rows

                    Dim cmdSQL = "INSERT INTO PRODUCTS(ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued) VALUES ('" _
                                & row.Cells(1).Value & "', '" _
                                & row.Cells(2).Value & "', '" _
                                & row.Cells(3).Value & "', '" _
                                & row.Cells(4).Value & "', '" _
                                & row.Cells(5).Value & "', '" _
                                & row.Cells(6).Value & "', '" _
                                & row.Cells(7).Value & "', '" _
                                & row.Cells(8).Value & "', '" _
                                & row.Cells(9).Value & "')"


                    cmd = New MySqlCommand(cmdSQL, con)
                    cmd.ExecuteNonQuery()
                Next
            Next
        End With
    End Sub

I got a problem making the loop for the progress bar?
I can't figure it out where to put exactly the loop for progress bar.
Please help me. Thanks in advance.
 
i already found a solution for this.

VB.NET:
With DataGridView1
            Dim a As Integer = 0         
            UltraProgressBar1.Maximum = DataGridView1.Rows.Count
            UltraProgressBar1.Value = ProgressBar1.Minimum
            For Each row As DataGridViewRow In .Rows
                Dim cmdSQL = "INSERT INTO PRODUCTS(ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued) VALUES ('" _
                            & row.Cells(1).Value & "', '" _
                            & row.Cells(2).Value & "', '" _
                            & row.Cells(3).Value & "', '" _
                            & row.Cells(4).Value & "', '" _
                            & row.Cells(5).Value & "', '" _
                            & row.Cells(6).Value & "', '" _
                            & row.Cells(7).Value & "', '" _
                            & row.Cells(8).Value & "', '" _
                            & row.Cells(9).Value & "')"


                cmd = New MySqlCommand(cmdSQL, con)
                cmd.ExecuteNonQuery()
                UltraProgressBar1.Value += 1             
            Next
        End With
 

Latest posts

Back
Top