I have the following code at button.click sub, it takes data from one access DB and load the rows one by one into a SQL DB, the line that inserts code is commented because it's already tested.
Dim cSQL As New SqlConnection(conSQL)
Dim fecha1, fecha2 As Date
Dim strfecha1, strfecha2 As String
For Each dr As DataRow In dt.Rows
fecha1 = CDate(dr.Item(0))
fecha2 = CDate(dr.Item(18))
strfecha1 = fecha1.ToString("yyyyMMdd hh:mm:ss tt").Replace(".", "")
strfecha2 = fecha2.ToString("yyyyMMdd hh:mm:ss tt").Replace(".", "")
selSQL = "insert into ttransactionlog1 ([OccurDateTime],[NodeID],[Kind],[UUID],[UserID],[FirstName],[LastName],[MI],[Department],[Rank],[CardID],[CreateDateTime],[LeftRight],[HD],[RotationDegree],[StableTimeInterval],[DecisionTimeInterval],[Message],[PIN]) values " + _
"('" + strfecha1 + "'," + _
dr.Item(1).ToString + "," + _
dr.Item(2).ToString + "," + _
dr.Item(4).ToString + ",'" + _
dr.Item(3).ToString + "','" + _
dr.Item(5).ToString + "','" + _
dr.Item(6).ToString + "','" + _
dr.Item(7).ToString + "','" + _
dr.Item(8).ToString + "','" + _
dr.Item(9).ToString + "','" + _
dr.Item(10).ToString + "','" + _
strfecha2 + "'," + _
dr.Item(16).ToString + "," + _
dr.Item(14).ToString + "," + _
dr.Item(15).ToString + "," + _
dr.Item(12).ToString + "," + _
dr.Item(13).ToString + ",'" + _
dr.Item(11).ToString + "'," + _
dr.Item(17).ToString + ")"
daSQL.InsertCommand = New SqlCommand(selSQL, cSQL)
'cSQL.Open()
'daSQL.InsertCommand.ExecuteNonQuery()
'cSQL.Close()
'DataGridView1.Rows.Insert(0, dr.ItemArray)
'If DataGridView1.Rows.Count > 10 Then
' DataGridView1.Rows.RemoveAt(10)
'End If
procesar = New Thread(AddressOf Me.actGrilla)
procesar.IsBackground = True
procesar.Start(dr)
'actGrilla(dr)
Next
I want to update a DGV showing the inserted datarow in it's top position and up to 10 records in screen, for that I'm trying to make a sub in a new thread in this way.
Sub actGrilla(ByVal dr As DataRow)
If InvokeRequired Then
Dim uud As New llamaactGrilla(AddressOf actGrilla)
Invoke(uud, dr)
Else
DataGridView1.Rows.Insert(0, dr.ItemArray)
If DataGridView1.Rows.Count > 10 Then
DataGridView1.Rows.RemoveAt(10)
End If
DataGridView1.Refresh()
End If
End Sub
My problem is when I try this way, the code always gone by invokeRequired true and collapse by memory fault (are about 5000 records), and when I try doing without a thread, writing the "else" code inside the for cycle the program eventually collapse with cotextswitchdeadlock.
What I'm doing wrong?