0

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
2
Contributors
5
Replies
25
Views
3 Years
Discussion Span
Last Post by androidz
0

Background threads cannot access controls directly. They have to use delegates. They are not that complicated and you can see an example of how to do that here

0

thanks Jim I appreciated your quick response. I'll update this thread for future reference. Thanks again.

0

Hi jim, thanks for your advice. I am able now to run my program with no errors. I forgot also to include workreportsprogress = true.

here is the piece of my code in case someone might need this.

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        m_bg.WorkerReportsProgress = True
        m_bg.WorkerSupportsCancellation = True

    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


        OpenSqlConnection()


        Dim dt As New DataTable

        If cn.State = ConnectionState.Closed Then cn.Open()

        Using da As New SqlClient.SqlDataAdapter(SQL, GetConnectionString)

            Dim count As Integer = da.Fill(dt)


            For i As Integer = 1 To count

                If Me.m_bg.CancellationPending Then
                    e.Cancel = True
                    Exit For
                End If

                System.Threading.Thread.Sleep(200)

                m_bg.ReportProgress(CInt((i / count) * 100))
                textcode(FormatPercent(i / count, 2))

            Next i

        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

            If dgv1.Rows.Count = 0 Then
                Me.m_bg.CancelAsync()
                MsgBox("Vendor Not Found in the table,Please check again.")
                ProgressBar1.Value = 0
                ProgressBar1.Visible = False
                Label4.Visible = False
                txtcode.Focus()
                Exit Sub
            End If
            dgv1.Columns("check").ReadOnly = False
            dgv1.Columns("upc").ReadOnly = True

            Dim IntI As Integer

            For IntI = 1 To dgv1.Rows.Count
                Label3.Text = (" " & IntI & " rows")

            Next IntI

            Label2.Text = "Query Completed"
            System.Threading.Thread.Sleep(500)
            ProgressBar1.Visible = False
            Label4.Visible = False
        End If
        btnSelectAll.Enabled = True

        '--credits to reverend jim--
         Private Delegate Sub dlgtextcode(ByVal text As String)

    Private Sub textcode(ByVal text As String)

        If Label4.InvokeRequired Then
            Dim dlg As New dlgtextcode(AddressOf textcode)
            Me.Invoke(dlg, text)
        Else
            Label4.Text = text
        End If

    End Sub
0

Ohh btw, i got exception saying time out. when im using this sql 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.1.104].Store.dbo.Vendor_products) and v.code = '" & txtcode.Text & "'order by vp.upc " checking between two servers.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.