我有三个 SQL 表:TGolfers、TEventYears 和 TGolferEventYears。TGolferEventYears 是哪个高尔夫球手参加了哪个赛事的表格。我已经成功填充了一个 Event Years 组合框 (cboEventYears),现在我正在尝试根据所选年份的高尔夫球手填充第二个 Golfers 组合框。但由于某种原因,我的 SELECT 语句抛出错误:“对象引用未设置为对象的实例。”
我不知道这意味着什么,有人可以帮忙吗?代码如下,谢谢。
'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