I have never used a background worker in vb.net, but from what I have read, I think it will help resolve an issue I am having during runtime. I am executing an sql statement, but it takes a bit to process. I think a background worker could help with this but I am unsure how to to implement this correctly. I greatly appreciate any help, tips, or comments. Thanks in advance!
Private Sub DeparmentButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn3EData.Click, Btn3WData.Click, Btn4EData.Click, BtnCCUData.Click, BtnERData.Click, BtnAllData.Click, BtnMatData.Click 'Declare variable Dim SelectedDeptButton As Button = CType(sender, Button) 'Sends Department Information Needed to build SQL statement FloorInfoNeeded(SelectedDeptButton.Tag) End Sub #Region "SQLInformation" Public Sub FloorInfoNeeded(ByVal DepartmentNumber As String) 'Declare variable Dim DepartmentsNeeded As String() = DepartmentNumber.Split("|") 'Base SQL string statement StringNeeded = "SELECT [Patient Account Number], [PV1 Room], [PV1 Financial Class], [First Name] + ' ' + MI + ' ' + [Last Name] [Patient Name], [PV1 Attending Phy Lastname], SUBSTRING([PV1 Admit Date/Time], 5, 2) + '/' + SUBSTRING([PV1 Admit Date/Time], 7, 2) + '/' + SUBSTRING([PV1 Admit Date/Time], 1, 4) [Admit Date], SUBSTRING([PV1 Admit Date/Time], 9, 4) [Admit Time], [PV1 Admit Source], [PV1 Discharge Date/Time] FROM ADT WHERE ((" 'Adds the Departments Needed to the SQL statement Dim i As Integer = 0 For Each dept As String In DepartmentsNeeded If i = 0 Then 'First department StringNeeded = StringNeeded & "[PV1 Department] = '" & dept & "' " Else 'Additional departments StringNeeded = StringNeeded & "OR [PV1 Department] = '" & dept & "' " End If i = i + 1 Next 'Ending section of SQL statement StringNeeded = StringNeeded & ") AND [PV1 Discharge Date/Time] = '') ORDER BY [PV1 Room]" 'Determines what appears in the label based on the department If i = 1 Then Select Case DepartmentsNeeded(0) Case "030" LblFloor.Text = "Three East" Case "032" LblFloor.Text = "Three West" Case "028" LblFloor.Text = "Four East" Case "042" LblFloor.Text = "Critical Care" Case "060" LblFloor.Text = "Emergency Department" End Select ElseIf i = 2 Then LblFloor.Text = "Maternity/Nursery" Else LblFloor.Text = "All Departments" End If 'Run the built SQL string RunSQL(StringNeeded) End Sub Private Sub RunSQL(ByVal SQLString As String) 'Declare variables Dim myConnStr As String = My.Settings.CPSISQLConnectionString 'Connection string to Database Dim table As New DataTable 'Storage for holding data 'Open and read Using con As New SqlConnection(myConnStr) con.Open() Dim cmd As New SqlCommand(SQLString, con) Dim reader As SqlDataReader reader = cmd.ExecuteReader table.Load(reader) With DataGridView1 .DataSource = table End With 'Close all reader.Close() con.Close() cmd.Dispose() End Using End Sub #End Region Private Sub bw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork 'TODO: Is this where RunSQL() should go? End Sub Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bw.ProgressChanged 'TODO: How does it determine if the progress has changed? End Sub Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted 'TODO: How does it determine if the process is completed? End Sub