Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with IndexOutOfRange Exception
#1
I keep getting this error when trying to run my program and click on something in a listbox.

IndexOutOfRangeException was unhandled Index was outside the bounds of the array.

My Code and the part where it errors (Marked with ><):

Code:
Dim conn As MySqlConnection
    conn = New MySqlConnection("server=db4free.net;port=3306; user id=*******; password=**********; database=*******")

    Dim sqlquery As String = "SELECT * FROM UTGAccess"
    Dim adapter As New MySqlDataAdapter
    Dim data As MySqlDataReader
    Dim command As New MySqlCommand
    conn.Open()
    command.CommandText = sqlquery
    command.Connection = conn
    adapter.SelectCommand = command
    data = command.ExecuteReader
    While data.Read()
        ListBox1.Items.Add(data(1).ToString)
        ListBox3.Items.Add(data(0).ToString)
        Dim writer As New System.IO.StreamWriter(My.Application.Info.DirectoryPath + "/" + data(1).ToString + "=" + data(0).ToString + ".txt", False)
        writer.Write(data(3).ToString + "=" + data(0).ToString)
        writer.Close()
    End While

    data.Close()
    conn.Close()

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    ListBox3.SelectedIndex = ListBox1.SelectedIndex
    Dim reader As New System.IO.StreamReader(My.Application.Info.DirectoryPath + "/" + ListBox1.SelectedItem.ToString + "=" + ListBox3.SelectedItem.ToString + ".txt")
    Dim splitread() As String = reader.ReadToEnd.Split("=")
    reader.Close()
  >lbl_rank1.Text = "Rank: " + splitread(3).ToString<
    lbl_id1.Text = "ID: " + splitread(0).ToString
End Sub
Note that it doesn't error on lbl_id1.text but it does on lbl_rank1.text. The ID is an INT on the site, while the rank is a VARCHAR. Both are numbers.
I tried rank with an INT too.

In the database:
0 = ID
1 = Username
2 = Password
3 = Rank
#2
All that really has to be looked at are these segments of code:

Code:
writer.Write(data(3).ToString + "=" + data(0).ToString)
Code:
Dim splitread() As String = reader.ReadToEnd.Split("=")
Code:
lbl_rank1.Text = "Rank: " + splitread(3).ToString

So what you are doing is writing two things into a text file, separated by an equals sign. Then you are reading that text file, and splitting up the elements by the equals sign. So you are splitting them up as
  • data(3).ToString
    data(0).ToString
Now, since this is an array, the first item (data(3)) will have an index of 0, and the second item (data(0)) will have an index of 1.

So with that in mind, you then try to get splitread(3).ToString. This throws an error because in the list, as we just saw, there are only items with an index of 0 and 1, and an item with the index of 3 does not exist.
My Blog | My Setup | My Videos | Have a wonderful day.
#3
So I should change the 3 to a 0 and the 0 to a 1 to get it to display correctly? Thanks for explaining that, was looking all over <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->
#4
Hm, well I don't know how your database tables are constructed, but just mess around until you get the result that you want!
My Blog | My Setup | My Videos | Have a wonderful day.


Forum Jump:


Users browsing this thread: 1 Guest(s)