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