Search code examples
mysqlvb.netbackgroundworker

vb.net backgroundworker with mysql reader to multiple textboxes


I want to run a BGW every 1 sec to run a mySQL query and fill the different textboxes in my form.

This is my functioning query:

Public Sub AnrufAutoLaden()
    Dim mysqlconn As New MySqlConnection(ConnectionString)
    mysqlconn.Open()
    Dim query As String = "SELECT   CCE.callerid Telefonnummer,
                                    date_format(CCE.datetime_init, '%d-%m-%Y') Datum,
                                    TIME_FORMAT(CCE.datetime_init, '%H:%i') Uhrzeit,
                                    A.Alias Agent, 
                                    CONVERT( CAST(CE.name AS BINARY) USING UTF8) Kunde          
                            FROM call_center.current_call_entry CCE, call_center.agent A, call_center.campaign_entry CE
                            where CE.estatus = 'A'
                            AND A.estatus = 'A'
                            AND A.id = CCE.id_agent
                            AND CE.id_queue_call_entry = CCE.id_queue_call_entry
                            AND A.name ='" & LabelUsername.Text & "';"
    Dim mycmdIDForm As New MySqlCommand(query, mysqlconn)
    Dim rdr As Object = mycmdIDForm.ExecuteReader()
    If rdr.read() Then
        TBDashBCallerID.Text = rdr("Telefonnummer").ToString
        TBDashBDatum.Text = rdr("Datum").ToString
        TBDashBZeit.Text = rdr("Uhrzeit").ToString
        TBDashBAgent.Text = rdr("Agent").ToString
        TBDashBKunde.Text = rdr("Kunde").ToString
    End If
    mysqlconn.Close()
End Sub

I was able to do it but for every textbox I need to run a separate BGW, like this:

Private Sub BGWAdminInfoV_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWAdminInfoV.DoWork
    Threading.Thread.Sleep(2000)
    If BGWAdminInfoV.CancellationPending = True Then
        e.Cancel = True
    Else
        Dim mysqlconn As New MySqlConnection(ConnectionString)
        mysqlconn.Open()
        Dim countquery2 As String = "SELECT     CCE.callerid Telefonnummer          
                            FROM call_center.current_call_entry CCE, call_center.agent A, call_center.campaign_entry CE
                            where CE.estatus = 'A'
                            AND A.estatus = 'A'
                            AND A.id = CCE.id_agent
                            AND CE.id_queue_call_entry = CCE.id_queue_call_entry
                            AND A.name ='" & LabelUsername.Text & "';;"
        Dim countcmd2 As MySqlCommand = New MySqlCommand(countquery2, mysqlconn)
        e.Result = countcmd2.ExecuteScalar().ToString()
        mysqlconn.Close()
    End If
End Sub
Private Sub BGWAdminInfoV_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGWAdminInfoV.RunWorkerCompleted
    If e.Cancelled = True Then
    Else
        Me.Invoke(New MethodInvoker(Sub() TBDashBCallerID.Text = e.Result))
        BGWAdminInfoV.RunWorkerAsync()
    End If
End Sub

But is there a way to do it with one BGW. So that I run one BGW every 1 second and return all five values to all five textboxes:

TBDashBCallerID.Text
TBDashBDatum.Text
TBDashBZeit.Text 
TBDashBAgent.Text
TBDashBKunde.Text

Solution

  • Why switch from ExecuteReader to ExecuteScalar? Just use the same ADO.NET code you already have but, instead of putting the values into the TextBoxes, put them into the properties of an object or the elements of an array. You then get all the values out of that same object/array in the RunWorkerCompleted event handler and populate all the TextBoxes.

    Kick things off like this:

    BGWAdminInfoV.RunWorkerAsync(LabelUsername.Text)
    

    and then do this:

    Private Sub BGWAdminInfoV_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWAdminInfoV.DoWork
        Dim mysqlconn As New MySqlConnection(ConnectionString)
        mysqlconn.Open()
        Dim query As String = "SELECT   CCE.callerid Telefonnummer,
                                    date_format(CCE.datetime_init, '%d-%m-%Y') Datum,
                                    TIME_FORMAT(CCE.datetime_init, '%H:%i') Uhrzeit,
                                    A.Alias Agent, 
                                    CONVERT( CAST(CE.name AS BINARY) USING UTF8) Kunde          
                            FROM call_center.current_call_entry CCE, call_center.agent A, call_center.campaign_entry CE
                            where CE.estatus = 'A'
                            AND A.estatus = 'A'
                            AND A.id = CCE.id_agent
                            AND CE.id_queue_call_entry = CCE.id_queue_call_entry
                            AND A.name = @name;"
        Dim mycmdIDForm As New MySqlCommand(query, mysqlconn)
        mycmdIDForm.Parameters.Add("@name", MySqlDbType.VarChar, 50).Value = e.Argument
        Dim rdr As Object = mycmdIDForm.ExecuteReader()
        Dim result As String()
        If rdr.Read() Then
            result = {rdr("Telefonnummer").ToString(),
                      rdr("Datum").ToString(),
                      rdr("Uhrzeit").ToString(),
                      rdr("Agent").ToString(),
                      rdr("Kunde").ToString()}
        End If
        mysqlconn.Close()
    
        e.Result = result
    End Sub
    
    Private Sub BGWAdminInfoV_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGWAdminInfoV.RunWorkerCompleted
        If Not e.Cancelled AndAlso e.Result IsNot Nothing Then
            Dim result = DirectCast(e.Result, String())
    
            TBDashBCallerID.Text = result(0)
            TBDashBDatum.Text = result(1)
            TBDashBZeit.Text = result(2)
            TBDashBAgent.Text = result(3)
            TBDashBKunde.Text = result(4)
        End If
    End Sub