Hi danibians,
wonder where i got this error. it got error after it reads the runcompleted process. here is my code
Hi Everyone,
Hope you could help me with this. I got an exception error saying "Exception has been thrown by the target of an invocation."
here is my code:
Imports System.Data.SqlClient
Imports System.Data
Imports System.ComponentModel
Public Class Form1
Dim m_countTo As Integer = 0
Dim bolUpc As Boolean
Dim cn As SqlConnection
Private WithEvents m_bg As New BackgroundWorker
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label3.Text = ""
End Sub
Private Sub txtcode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtcode.TextChanged
bolUpc = False
If bolUpc = False Then
Me.dgv1.DataSource = Nothing
Me.btnSelectAll.Text = "Select All"
Me.btnSelectAll.Enabled = False
Label3.Text = ""
End If
End Sub
Private Sub txtcode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtcode.KeyDown
If e.KeyCode = 13 Then
Try
cn = New SqlConnection(GetConnectionString)
Dim SQL As String = "Select vp.upc from vendor_products vp join vendor_profile v on vp.vendor_id = v.id where vp.id not in (Select id from [192.168.180.104].Store.dbo.Vendor_products) and v.code = '" & txtcode.Text & "'order by vp.upc "
m_bg.RunWorkerAsync(SQL)
Catch ex As Exception
Me.m_bg.CancelAsync()
MsgBox(ex.Message)
End Try
btnSelectAll.Enabled = True
End If
End Sub
Private Sub OpenSqlConnection()
Dim connectionstring As String = GetConnectionString()
Using connection As New SqlConnection(connectionstring)
connection.Open()
End Using
End Sub
Private Function GetConnectionString() As String
Return "Data Source = (local);Database=Store;User Id=sa;Asynchronous processing = true;"
End Function
Private Sub btnSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectAll.Click
Dim intI As Integer
Dim ctl As New Control
intI = 0
If btnSelectAll.Text = "Select All" Then
For Each row As DataGridViewRow In dgv1.Rows
If row.Cells("check").Value = False Then
dgv1.Rows(intI).Cells(0).Value = True
intI += 1
End If
Next
btnSelectAll.Text = "Unselect All"
Exit Sub
Else
For Each row As DataGridViewRow In dgv1.Rows
If row.Cells("check").Value = True Then
dgv1.Rows(intI).Cells(0).Value = False
intI += 1
End If
Next
btnSelectAll.Text = "Select All"
End If
End Sub
Private Sub m_bg_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles m_bg.DoWork
Dim SQL As String = e.Argument.ToString
OpenSqlConnection()
Dim dt As New DataTable
Using da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(SQL, cn)
Dim count As Integer = da.Fill(dt)
For i As Integer = 0 To count
If Me.m_bg.CancellationPending Then
e.Cancel = True
Exit For
End If
System.Threading.Thread.Sleep(250)
Me.m_bg.ReportProgress(CInt((i / count) * 100))
SetLabeltext_Threadsafe(Me.Label2, FormatPercent(i / count, 2))
Next
End Using
e.Result = dt
End Sub
Private Sub m_bg_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles m_bg.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub m_bg_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles m_bg.RunWorkerCompleted
'If e.Cancelled Then
' Me.Label2.Text = "Cancelled"
'Else
Dim dt As DataTable = e.Result
dgv1.DataSource = dt
'Me.Label2.Text = "Completed"
'End If
End Sub
Delegate Sub SetLabeltext_Delegate(ByVal [Label] As Label, ByVal [text] As String)
Private Sub SetLabeltext_Threadsafe(ByVal [Label] As Label, ByVal [text] As String)
If [Label].InvokeRequired Then
Dim MyDelegate As New SetLabeltext_Delegate(AddressOf SetLabeltext_Threadsafe)
Me.Invoke(MyDelegate, New Object() {[Label], [text]})
Else
[Label].Text = [text]
End If
End Sub
End Class