I am not sure whether this should be under .NET or Database.
I am attempting to populate a combobox from a table contained in the database being used for a project.
The query is no problem. It recovers the 8 records currently residing in the table lkpDept.
The For/Next code I obtained from a reference source and I do not fully understand its workings. When the method is executed I get an exception, "System.IndexOutOfRangeException: 'Cannot find table 0.'"
From the line, For Each...
I surmise that there might be a problem with ListDataSet, but I have no idea what that might be.Code:Private Sub GetDeptList() MyQuery = "Department ComboBox query" cboOwner.Items.Clear() MasterBase.MasterBaseQuery("SELECT colDept FROM lkpDept") If RecordCount > 0 Then For Each r As DataRow In MasterBase.ListDataSet.Tables(0).Rows cboOwner.Items.Add(r("colDept")) Next cboOwner.ValueMember = "colDept" cboOwner.DisplayMember = "colDept" cboOwner.SelectedIndex = -1 ElseIf MasterBase.Exception <> "" Then MsgBox(MasterBase.Exception) End If End Sub
ListDataSet is defined in the MasterBaseConn class as shown below. All I can think of is that I have somehow done something wrong in making the dataset, but I do not know what that could be.
Code:Public Class MasterBaseConn Public MasterBaseConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MasterBase4.0.accdb;") 'Database Connection Public ListCommand As New OleDbCommand Public ListAdapter As OleDbDataAdapter Public ListTable As DataTable Public ListDataSet As DataSet Public Params As New List(Of OleDbParameter) Public Exception As String Public Sub MasterBaseQuery(SetQuery As String) RecordCount = 0 Exception = "" Try MasterBaseConnection.Open() 'Open connection ListCommand = New OleDbCommand(SetQuery, MasterBaseConnection) 'Database Command Params.ForEach(Sub(p) ListCommand.Parameters.Add(p)) 'Load params into command Params.Clear() 'Clear params list ListDataSet = New DataSet ListTable = New DataTable ListAdapter = New OleDbDataAdapter(ListCommand) RecordCount = ListAdapter.Fill(ListTable) Catch ex As Exception Exception = ex.Message MsgBox(ex.Message + vbLf + vbCrLf + MyQuery + " failed to execute.") End Try If MasterBaseConnection.State = ConnectionState.Open Then MasterBaseConnection.Close() End Sub Public Sub AddParam(Name As String, Value As Object) Dim NewParam As New OleDbParameter(Name, Value) Params.Add(NewParam) End Sub End Class




Reply With Quote
