IndexOutOfRangeException?

korae

Member
Joined
Jan 24, 2010
Messages
22
Programming Experience
Beginner
I have this code that adds items in 2 comboboxes:
The first code works fine as it adds the dates into the comboxes

VB.NET:
Dim myConnection As OleDbConnection = New OleDbConnection
            Dim myCommand As OleDbCommand = New OleDbCommand
            Dim myDataReader As OleDbDataReader
            myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Network_Info.mdb"
            Try
                myConnection.Open()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            ComboBox2.Items.Clear()
            ComboBox3.Items.Clear()
            myCommand.Connection = myConnection
            myCommand.CommandText = "Select Distinct DateRecorded from Computer_Connection_Info Order by DateRecorded"
            myDataReader = myCommand.ExecuteReader()
            ComboBox2.Items.Clear()
            ComboBox3.Items.Clear()
            While (myDataReader.Read)
                ComboBox2.Items.Add(myDataReader("DateRecorded"))
                ComboBox3.Items.Add(myDataReader("DateRecorded"))
            End While
            myDataReader.Close()

But when I try to add items by month I always get this error in these lines:

ComboBox2.Items.Add(myDataReader("DateRecorded"))
ComboBox3.Items.Add(myDataReader("DateRecorded"))

It says: IndexOutOfRangeException was unhandled

VB.NET:
Dim myConnection As OleDbConnection = New OleDbConnection
            Dim myCommand As OleDbCommand = New OleDbCommand
            Dim myDataReader As OleDbDataReader
            myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Network_Info.mdb"
            Try
                myConnection.Open()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            ComboBox2.Items.Clear()
            ComboBox3.Items.Clear()
            myCommand.Connection = myConnection
            myCommand.CommandText = "Select Distinct format([DateRecorded], 'MMM/YYYY') from Computer_Connection_Info Order by format([DateRecorded], 'MMM/YYYY') DESC"
            myDataReader = myCommand.ExecuteReader()
            ComboBox2.Items.Clear()
            ComboBox3.Items.Clear()
            While (myDataReader.Read)
                ComboBox2.Items.Add(myDataReader("DateRecorded"))
                ComboBox3.Items.Add(myDataReader("DateRecorded"))
            End While
            myDataReader.Close()
 
I got it now guys. The problem was my second code snippet doesn't seem to recognize the column so I just added:

format([DateRecorded], 'MMM/YYYY') As [Date Recorded]

And its working now!
 
Back
Top