none
Recieving a NullRefrence Error Even though the connection is set up properly RRS feed

  • Question

  • Hi, I was wondering why my loadEmployees sub will not run properly in the background, despite the connections being properly opened and the class being implemented. Thank you!

       Public Sub LoadEmployees()
            If loadEmployeesAsync.IsBusy <> True Then
                loadEmployeesAsync.RunWorkerAsync()
            End If
        End Sub
    
        Private Sub loadEmployeesAsync_DoWork(sender As Object, e As DoWorkEventArgs) Handles loadEmployeesAsync.DoWork
            If loadEmployeesAsync.CancellationPending = True Then
                e.Cancel = True
            Else
                employeeDataSet = dConnect.loadDataGrid("*")
            End If
    
        End Sub
    
        Private Sub loadEmployeesAsync_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles loadEmployeesAsync.RunWorkerCompleted
            If e.Cancelled = True Then
                MsgBox("Cancelled!")
            ElseIf e.Error IsNot Nothing Then
                MsgBox("Error: " & e.Error.Message)
    
            Else
    
                'TO DO : dataset is available. Populate Employee List
    
                For Each row In employeeDataSet.Tables("Employees").Rows
    
                    Dim tEmployee As New Employee
                    tEmployee.ID = row("ID")
                    tEmployee.Name = row("Description")
                    tEmployee.Address = row("Address")
    
                    AddEmployee(tEmployee)
                Next
    
                RaiseEvent loadCompleted()
    
            End If
        End Sub

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If bgwdata.IsBusy <> True Then
                bgwdata.RunWorkerAsync()
            End If
    
        End Sub
    
    
    
      Private Sub bgwdata_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgwdata.DoWork
            Employees.LoadEmployees()
        End Sub
    
        Private Sub bgwdata_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgwdata.RunWorkerCompleted
    
        End Sub


    Tuesday, July 31, 2018 9:07 PM

All replies

  • What do you mean by "run properly"?  What do you expect to happen versus what actually happens?

    Just guessing here, but what does AddEmployee() do?  Interact with a user control like a DataGridView?  If so (and you have CheckForIllegalCrossThreadCalls set to false somewhere, which should never be used), that would cause blocking and make the program "freeze"... if that's what you mean by not working properly.

    -edit-

    I suppose it could also be interacting with something that is data bound, in which case you might not get the cross thread error, but could be facing binding updates causing blocking.  If that were the case it would be a matter of suspending the binding before running the routine and resuming it afterward.  Still just guessing though since it isn't clear what the problem is.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, July 31, 2018 9:37 PM
    Moderator
  • Sorry by run properly, I mean the sql database being loaded to the datagrid. Currently the issue is when the form is loaded, the datatable from sql is not being displayed to the form. 
    Tuesday, July 31, 2018 9:51 PM
  • Sorry by run properly, I mean the sql database being loaded to the datagrid. Currently the issue is when the form is loaded, the datatable from sql is not being displayed to the form. 

    Sorry, I got side tracked looking at the code and forgot the error you mention in the title.  What line of code is causing the null reference exception?

    Also, what is this method? dConnect.loadDataGrid("*") I would guess that this is where the problem is.

    Nevermind my previous reply... I missed that the AddEmployee was in the completed event which is back on the main thread.  That said, it isn't clear which part of this code is meant to cause the actual binding to the data grid... is that in AddEmployee or in your handler for the loadCompleted event you created?  I'm guessing the issue(s) are in the code we haven't seen yet.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, July 31, 2018 10:17 PM
    Moderator