title: Index out of bounds of the array vb.net
body: <p>I am getting an error at this line:</p>
  
  <pre><code>        CheckedListBox3.Items.Remove(CheckedListBox3.CheckedItems(0))
  </code></pre>
  
  <p>I don't understand why because it does read the first two but not the third... the index of my checkedlistbox1, 2 and 3 is 3 so that makes it (0), (1), and (2) in each while...</p>
  
  <p>Please help me out and tell me where i am wrong because i don't understand how it's possible that he can find the index of the first and second listbox but can't find the index of the third listbox</p>
  
  <p>This is my code:</p>
  
  <pre><code> cmd = New SqlCommand("SELECT serienaam, ouderdom, aantal FROM klantserie Where klantnummer = '" &amp; klantnummer &amp; "' ", con)
  
          Dim ttr As SqlDataReader = cmd.ExecuteReader()
  
          While ttr.Read = True
  
              CheckedListBox1.Items.Add(ttr.Item("serienaam"))   "serienaam" = a-map or c-map
              CheckedListBox2.Items.Add(ttr.Item("ouderdom"))    "ouderdom" = week1 - week16
              CheckedListBox3.Items.Add(ttr.Item("aantal"))      "aantal" =  1 - 10
  
          End While
  </code></pre>
  
  <p>added the code that adds the items to the checkedlistbox</p>
  
  <pre><code>    Private Sub Button79_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button79.Click
  
  
  
      While CheckedListBox1.CheckedItems.Count - 1 &gt;= 0     ''i have changed this line a few times don't really know if it matters to the outcome of the error''
  
          Dim serienaam
          Dim oud
  
          Dim aantal As Integer
          Dim bedrag As Decimal
          Dim totaal As Decimal
  
          Try
              serienaam = CheckedListBox1.CheckedItems(0)
              oud = CheckedListBox2.CheckedItems(0)
              aantal = CheckedListBox3.CheckedItems(0)
          Catch ex As Exception
  
          End Try
  
  
          cmd = New SqlCommand("SELECT " &amp; oud &amp; " FROM series Where naamserie = '" &amp; serienaam &amp; "' ", con)
  
          Dim sdr As SqlDataReader = cmd.ExecuteReader()
  
          If sdr.Read = True Then
              Dim week = sdr.Item(oud)
              bedrag = week * aantal
              totaal = Label69.Text
              totaal = totaal - bedrag
  
              sdr.Close()
              Label69.Text = totaal
  
          End If
  
  
  
          CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
  
          CheckedListBox2.Items.Remove(CheckedListBox2.CheckedItems(0))
          Try
              CheckedListBox3.Items.Remove(CheckedListBox3.CheckedItems(0))   ''&lt;&lt;&lt;----error
          this try doesn't do anything actually except for the fact that im not getting any errors anymore 
          Catch ex As Exception
              If ex IsNot Nothing Then
                  Dim s = CheckedListBox3.Items.Count
                  CheckedListBox3.SelectedItem = s
                  CheckedListBox3.Items.Remove(s)
  
              End If
          End Try
  
  
  
      End While
  
  End Sub
  </code></pre>
  
  <p>this is the code for the check</p>
  
  <pre><code>    Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
  
      Dim index As Integer = e.Index
  
      CheckedListBox2.SetItemChecked(index, e.NewValue)
      CheckedListBox3.SetItemChecked(index, e.NewValue)
  
  End Sub
  </code></pre>
  
  <p>my solution since it didn't work.</p>
  
  <pre><code>           Private Sub Button76_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button76.Click
      Dim serienaam
      Dim oud
      Dim aantal As Integer
      Dim bedrag As Decimal
      Dim totaal As Decimal
      Dim t As String
  
      serienaam = ComboBox6.SelectedItem
      oud = ComboBox7.SelectedItem
      aantal = TextBox45.Text
  
  
  
  
      cmd = New SqlCommand("SELECT " &amp; oud &amp; " FROM series Where naamserie = '" &amp; serienaam &amp; "' ", con)
  
      Dim sdr As SqlDataReader = cmd.ExecuteReader()
  
      If sdr.Read = True Then
          Dim week = sdr.Item(oud)
          bedrag = week
          totaal = Label69.Text
          totaal = totaal + bedrag * aantal
  
  
  
          sdr.Close()
          Label69.Text = totaal
          t = " '" &amp; serienaam &amp; "'                     '" &amp; oud &amp; "'                         '" &amp; aantal &amp; "' "
          CheckedListBox1.Items.Add(t)
  
  
  
      End If
  
  
  End Sub
  
            Private Sub Button79_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button79.Click
  
  
  
      While CheckedListBox1.CheckedItems.Count &lt;&gt; 0
  
          Dim klantnr As String
          Dim serienaam As String
          Dim oud As String
          Dim list As String
          Dim aantal As Integer
          Dim bedrag As Decimal
          Dim totaal As Decimal
          klantnr = TextBox32.Text
  
          list = CheckedListBox1.CheckedItems(0)
          Dim strlist = list.Split("'")
          For count = 0 To strlist.Length - 1
              serienaam = strlist(1)
              oud = strlist(3)
              aantal = strlist(5)
          Next
  
          cmd = New SqlCommand("SELECT " &amp; oud &amp; " FROM series Where naamserie = '" &amp; serienaam &amp; "' ", con)
  
          Dim sdr As SqlDataReader = cmd.ExecuteReader()
  
          If sdr.Read = True Then
              Dim week = sdr.Item(oud)
              bedrag = week * aantal
              totaal = Label69.Text
              totaal = totaal - bedrag
  
              sdr.Close()
              Label69.Text = totaal
  
          End If
  
          cmd = New SqlCommand("DELETE FROM klantserie Where klantnummer = '" &amp; klantnr &amp; "' And serienaam = '" &amp; serienaam &amp; "' And ouderdom = '" &amp; oud &amp; "' And aantal = '" &amp; aantal &amp; "' ", con)
  
          If con.State = ConnectionState.Closed Then con.Open()
  
          cmd.ExecuteNonQuery()
  
          ShowData()
  
          CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
  
      End While
  
  End Sub
  </code></pre>