title: Exception Unhandle System.IndexOutOfRangeException
body: <p>This code is about viewing a published consultation schedule. Unfortunately I'm stuck with the if statement. The error occurs at line : If dr("Lecturer") = ComboBox2.SelectedItem Then || I'm using a combobox to display the data from a database, which means there is a list of items in the combobox, and I will select any lecturer so it will display into the form. I have a database that contained the published schedule inside as shown at the pic. And all data stored in here(database) are actually unavailable(declared as red=unavailable, lime=available), except for the available does not stored into the database. So, what I'm doing now is to read and match the "Time", "Available" and "Lecturer" from the database, and change it to (red color) into the respective label(in lime colored).</p>
  
  <p><a href="https://i.stack.imgur.com/Imy2q.png" rel="nofollow noreferrer">Database</a></p>
  
  <p><a href="https://i.stack.imgur.com/vJejJ.png" rel="nofollow noreferrer">The Form</a></p>
  
  <pre><code>    Imports System.Data.OleDb
  Public Class viewconsultationschedule
  Dim time1 As String = "8.00am-10.00am"
  Dim time2 As String = "10.00am-12.00pm"
  Dim time3 As String = "12.00pm-2.00pm"
  Dim time4 As String = "2.00pm-4.00pm"
  Dim time5 As String = "4.00pm-6.00pm"
  Dim day1 As String = "Monday"
  Dim day2 As String = "Tuesday"
  Dim day3 As String = "Wednesday"
  Dim day4 As String = "Thursday"
  Dim day5 As String = "Friday"
  
  Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=sdpdatabase.accdb;Persist Security Info=False;")
  Dim cmd As OleDbCommand
  Dim cmd2 As OleDbCommand
  Dim dr As OleDbDataReader
  Dim sql As String
  Dim sql2 As String
  Private Sub ToolStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
  
  End Sub
  
  Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
      'If (ComboBox2.SelectedIndex = 3) Then
  
      Dim time1 As String = "8.00am-10.00am"
          Dim time2 As String = "10.00am-12.00pm"
          Dim time3 As String = "12.00pm-2.00pm"
          Dim time4 As String = "2.00pm-4.00pm"
          Dim time5 As String = "4.00pm-6.00pm"
          Dim day1 As String = "Monday"
          Dim day2 As String = "Tuesday"
          Dim day3 As String = "Wednesday"
          Dim day4 As String = "Thursday"
          Dim day5 As String = "Friday"
          Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=sdpdatabase.accdb;Persist Security Info=False;")
          sql = "Select * FROM consultationschedule WHERE Lecturer=@Lecturer" ' And [Time]=@Time AND weekDay=@weekDay "
          sql2 = "Select COUNT (*) FROM consultationschedule WHERE Lecturer=@lecturer"
          conn.Open()
          cmd = New OleDbCommand(sql, conn)
          cmd2 = New OleDbCommand(sql2, conn)
  
          cmd.Parameters.AddWithValue("@lecturer", ComboBox2.SelectedItem)
          'cmd.Parameters.AddWithValue("@[Time]", time1)
          'cmd.Parameters.AddWithValue("@weekDay", day1)
  
          cmd2.Parameters.AddWithValue("@lecturer", ComboBox2.SelectedItem)
          'cmd2.Parameters.AddWithValue("@[Time]", time2)
          'cmd2.Parameters.AddWithValue("@weekDay", day2)
          Dim count As Integer = cmd2.ExecuteScalar()
  
          dr = cmd.ExecuteReader
          dr = cmd2.ExecuteReader
          If (dr.HasRows) Then
          For i = 1 To count
              If (i = 1) Then
                  dr.Read()
  
                  If dr("Lecturer") = ComboBox2.SelectedItem Then
  
                      If dr("Available") = "1" Then
                          Label11.BackColor = Color.Red
  
                      Else
                          Label11.BackColor = Color.Lime
                      End If
  
                  ElseIf (i = 2) Then
                      If dr("Lecturer") = ComboBox2.SelectedItem Then
                          If dr("Available") = "1" Then
                              Label12.BackColor = Color.Red
  
                          Else
                              Label12.BackColor = Color.Lime
  
  
                          End If
  
  
                      ElseIf (i = 3) Then
                          If dr("Lecturer") = ComboBox2.SelectedItem Then
                              If dr("Available") = "1" Then
                                  Label13.BackColor = Color.Red
  
                              Else
                                  Label13.BackColor = Color.Lime
  
  
                              End If
  
  
  
                          ElseIf (i = 4) Then
                              If dr("Lecturer") = ComboBox2.SelectedItem Then
                                  If dr("Available") = "1" Then
                                      Label14.BackColor = Color.Red
  
                                  Else
                                      Label14.BackColor = Color.Lime
  
  
                                  End If
  
                              ElseIf (i = 5) Then
                                  If dr("Lecturer") = ComboBox2.SelectedItem Then
                                      If dr("Available") = "1" Then
                                          Label15.BackColor = Color.Red
  
                                      Else
                                          Label15.BackColor = Color.Lime
  
  
                                      End If
                                  ElseIf (i = 6) Then
                                      If dr("Lecturer") = ComboBox2.SelectedItem Then
                                          If dr("Available") = "1" Then
                                              Label16.BackColor = Color.Red
  
                                          Else
                                              Label16.BackColor = Color.Lime
  
  
                                          End If
                                      ElseIf (i = 7) Then
                                          If dr("Lecturer") = ComboBox2.SelectedItem Then
                                              If dr("Available") = "1" Then
                                                  Label17.BackColor = Color.Red
  
                                              Else
                                                  Label17.BackColor = Color.Lime
  
  
                                              End If
                                          ElseIf (i = 8) Then
                                              If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                  If dr("Available") = "1" Then
                                                      Label18.BackColor = Color.Red
  
                                                  Else
                                                      Label18.BackColor = Color.Lime
  
  
                                                  End If
                                              ElseIf (i = 9) Then
                                                  If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                      If dr("Available") = "1" Then
                                                          Label19.BackColor = Color.Red
  
                                                      Else
                                                          Label19.BackColor = Color.Lime
  
  
                                                      End If
                                                  ElseIf (i = 10) Then
                                                      If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                          If dr("Available") = "1" Then
                                                              Label20.BackColor = Color.Red
  
                                                          Else
                                                              Label20.BackColor = Color.Lime
  
                                                          End If
                                                      ElseIf (i = 11) Then
                                                          If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                              If dr("Available") = "1" Then
                                                                  Label21.BackColor = Color.Red
  
                                                              Else
                                                                  Label21.BackColor = Color.Lime
  
                                                              End If
                                                          ElseIf (i = 12) Then
                                                              If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                  If dr("Available") = "1" Then
                                                                      Label22.BackColor = Color.Red
  
                                                                  Else
                                                                      Label22.BackColor = Color.Lime
  
                                                                  End If
                                                              ElseIf (i = 13) Then
                                                                  If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                      If dr("Available") = "1" Then
                                                                          Label23.BackColor = Color.Red
  
                                                                      Else
                                                                          Label23.BackColor = Color.Lime
                                                                      End If
                                                                  ElseIf (i = 14) Then
                                                                      If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                          If dr("Available") = "1" Then
                                                                              Label24.BackColor = Color.Red
  
                                                                          Else
                                                                              Label24.BackColor = Color.Lime
  
                                                                          End If
                                                                      ElseIf (i = 15) Then
                                                                          If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                              If dr("Available") = "1" Then
                                                                                  Label25.BackColor = Color.Red
  
                                                                              Else
                                                                                  Label25.BackColor = Color.Lime
  
                                                                              End If
                                                                          ElseIf (i = 16) Then
                                                                              If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                  If dr("Available") = "1" Then
                                                                                      Label26.BackColor = Color.Red
  
                                                                                  Else
                                                                                      Label26.BackColor = Color.Lime
                                                                                  End If
                                                                              ElseIf (i = 17) Then
                                                                                  If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                      If dr("Available") = "1" Then
                                                                                          Label27.BackColor = Color.Red
  
                                                                                      Else
                                                                                          Label27.BackColor = Color.Lime
                                                                                      End If
                                                                                  ElseIf (i = 18) Then
                                                                                      If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                          If dr("Available") = "1" Then
                                                                                              Label28.BackColor = Color.Red
  
                                                                                          Else
                                                                                              Label28.BackColor = Color.Lime
                                                                                          End If
                                                                                      ElseIf (i = 19) Then
                                                                                          If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                              If dr("Available") = "1" Then
                                                                                                  Label29.BackColor = Color.Red
  
                                                                                              Else
                                                                                                  Label29.BackColor = Color.Lime
  
                                                                                              End If
  
                                                                                          ElseIf (i = 20) Then
                                                                                              If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                                  If dr("Available") = "1" Then
                                                                                                      Label30.BackColor = Color.Red
  
                                                                                                  Else
                                                                                                      Label30.BackColor = Color.Lime
                                                                                                  End If
                                                                                              ElseIf (i = 21) Then
                                                                                                  If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                                      If dr("Available") = "1" Then
                                                                                                          Label33.BackColor = Color.Red
  
                                                                                                      Else
                                                                                                          Label33.BackColor = Color.Lime
                                                                                                      End If
                                                                                                  ElseIf (i = 22) Then
                                                                                                      If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                                          If dr("Available") = "1" Then
                                                                                                              Label34.BackColor = Color.Red
  
                                                                                                          Else
                                                                                                              Label34.BackColor = Color.Lime
                                                                                                          End If
                                                                                                      ElseIf (i = 23) Then
                                                                                                          If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                                              If dr("Available") = "1" Then
                                                                                                                  Label35.BackColor = Color.Red
  
                                                                                                              Else
                                                                                                                  Label35.BackColor = Color.Lime
                                                                                                              End If
                                                                                                          ElseIf (i = 24) Then
                                                                                                              If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                                                  If dr("Available") = "1" Then
                                                                                                                      Label36.BackColor = Color.Red
  
                                                                                                                  Else
                                                                                                                      Label36.BackColor = Color.Lime
  
                                                                                                                  End If
                                                                                                              ElseIf (i = 25) Then
                                                                                                                  If dr("Lecturer") = ComboBox2.SelectedItem Then
                                                                                                                      If dr("Available") = "1" Then
                                                                                                                          Label37.BackColor = Color.Red
  
                                                                                                                      Else
                                                                                                                          Label37.BackColor = Color.Lime
  
                                                                                                                      End If
  
                                                                                                                  End If
                                                                                                              End If
                                                                                                          End If
                                                                                                      End If
                                                                                                  End If
                                                                                              End If
                                                                                          End If
                                                                                      End If
                                                                                  End If
                                                                              End If
                                                                          End If
                                                                      End If
                                                                  End If
                                                              End If
                                                          End If
                                                      End If
                                                  End If
                                              End If
                                          End If
                                      End If
                                  End If
                              End If
                          End If
                      End If
                  End If
              End If
          Next
      End If
  </code></pre>