-1

I have three SQL tables: TGolfers, TEventYears, and TGolferEventYears. TGolferEventYears is a table of which golfer played in which event. I have successfully populated an Event Years combo box (cboEventYears), now I am trying to populate a second Golfers combo box based on the golfers in the year selected. But for some reason, my SELECT statement is throwing the error: 'Object reference not set to an instance of an object.'
I have no idea what that means, can anyone help? Code below, thanks.

'Load golfers combo box based on event year
Private Sub cboEventYears_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboEventYears.SelectedIndexChanged

    Try

        Dim strSelect As String = ""
        Dim cmdSelect As OleDb.OleDbCommand 'Used for Select Statement
        Dim drSourceTable As OleDb.OleDbDataReader 'Where data is retrieved to
        Dim dt As DataTable = New DataTable 'Table we will load from our reader

        'Open the DB
        If OpenDatabaseConnectionSQLServer() = False Then

            'If not, warn user
            MessageBox.Show(Me, "Database connection error." & vbNewLine &
                                    "The application will now close.",
                                    Me.Text + " Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error)

            'Close form
            Me.Close()
        End If

        'Build Select Statement 
        strSelect = "SELECT TGolfers.intGolferID, TGolfers.strLastName FROM TGolferEventYears JOIN TGolfers ON TGolferEventYears.intGolferID = TGolfers.intGolferID WHERE TGolferEventYears.intEventYearID = " & cboEventYears.SelectedValue.ToString

        'Retrieve all records
        cmdSelect = New OleDb.OleDbCommand(strSelect, m_conAdministrator)
        drSourceTable = cmdSelect.ExecuteReader

        'Load Table from Data Reader
        dt.Load(drSourceTable)

        'Add item to Combo Box. We need golferID to be associated
        'with the last name in order to pull the rest of the data.
        'Binding column name to the combo box display and value members.
        cboGolfers.ValueMember = "TGolfers.intGolferID"
        cboGolfers.DisplayMember = "TGolfers.strLastName"
        cboGolfers.DataSource = dt

        'Select first item in the list by default
        If cboGolfers.Items.Count > 0 Then cboGolfers.SelectedIndex = -1


     Catch excError As Exception

         'Log and display error message
         MessageBox.Show(excError.Message)

     End Try
End Sub
Naim Jamalludin
  • 195
  • 3
  • 16
  • No, I understand now it's some sort of null error, but not really sure what that means or why it's happening. Thanks anyway – jennifer jonas Aug 21 '20 at 03:21
  • The answers to that question give you all the information anyone can give you without actually debugging your system. This is an essential concept for coding in .NET so I recommend investing the time to understand it and learn to debug it. – Dale K Aug 21 '20 at 03:24
  • Try to place a break point on the combo box selected value. Does it return a selected value? If so, place another break point on Execute Reader just to see whether there's an open connection or not. Last thing, place another break point on the returned datatable – Naim Jamalludin Aug 21 '20 at 03:37
  • 2
    Imagine you're cooking a meal and the recipe calls for a teaspoon of salt. You grab your salt shaker and try to add salt to the mix. Huh? Nothing comes out! You're out of salt! This is a real-world equivalent to a `NullReferenceException`: you've tried to use something that isn't there. The secret with any `NullReferenceException` is to debug and find out what isn't there that should be (i.e. find what's `null`). – ProgrammingLlama Aug 21 '20 at 03:39
  • OT if you are using SqlServer (as suggested by your OpenDatabaseConnectionSQLServer), there are also SqlConnection and SqlCommand objects that may perform better than the OleDb ones. See https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient – Hans Kesting Aug 21 '20 at 06:54

1 Answers1

0

most probably you are referencing a null value in your select statement, I would say that cboEventYears.SelectedValue.ToString is your culprit. Try your code again but with a constant instead of this and see if this fixes. And you can also try cboEventYears.text I have noticed it's usually the better option.

Mohammed
  • 151
  • 1
  • 12