views:

137

answers:

4

I'm trying to create two arrays filled with a set of strings from twos tables in a database and then compare them. For example (array1[0]=1101 and array2[0]=0110). If the both respective characters =1 then perform an action. But when I run this code I receive the error Index was outside the bounds of the array. System.IndexOutOfRangeException. For some reason I believe the problem area is the two statements :

comparestringa = userintarray(x)

comparestringb = eventintarray(x)

When i comment out them..the error doesn't show.

    myconnect = New SqlConnection("xxxx")
    Dim Table1 As New SqlCommand("SELECT * FROM Table1", myconnect)
    Dim Table2 As New SqlCommand("SELECT * FROM Table2", myconnect)

    Dim array1 As New ArrayList
    Dim array2 As New ArrayList

    Table1.Connection.Open()
    Dim r As SqlDataReader
    r = Table1.ExecuteReader(CommandBehavior.CloseConnection)
    While r.Read
        array1.Add(r(1).ToString())
        array1.Add(r(2).ToString())
        array1.Add(r(3).ToString())
        array1.Add(r(4).ToString())

    End While
    r.Close()
    myconnect.Close()



    Table2.Connection.Open()
    Dim r2 As SqlDataReader
    r2 = Table2.ExecuteReader(CommandBehavior.CloseConnection)
    While r2.Read
        array2.Add(r2(1).ToString())
        array2.Add(r2(2).ToString())
        array2.Add(r2(3).ToString())
        array2.Add(r2(4).ToString())

    End While
    r2.Close()
    myconnect.Close()



    Dim comparestringa, comparestringb As String
    Dim compare_string_counter As Int16 = 1

    comparestringa = userintarray(0)
    comparestringb = eventintarray(0)


     For x = 0 To array1.Count - 1

        If comparestringa(x) = "1" And comparestringa(x) = comparestringb(x) Then

              Label4.Text = Label4.Text + " 1 "

        Else
            compare_string_counter = compare_string_counter + 1
        End If
        comparestringa = userintarray(x)
        comparestringb = eventintarray(x)

    Next
A: 

Your

userintarray(x)

eventintarray(x)

Are not defined in your code snippet. Can you check whether you allocate enough array items on them before you use them?

Ngu Soon Hui
those arrays are actually array1 and array 2 respectively
Chris Allen
A: 

userintarray and eventintarray are not declared (at least not in the code you posted.) Apparently they are dimensioned too small, or they are not dimensioned at all.

xpda
those arrays are actually array1 and array 2 respectively
Chris Allen
A: 

The code that loops through the arrays has only one index variable and uses it for both arrays. Given the index out of bounds error, I suspect that the arrays do not contain the same number of items. Since the index is based on array1, it's likely that array2 is smaller and thus the index is invalid.

Corin
A: 

Assuming your "select * from x" (really, fill in the column names!) returns 4 values you'll have to do:

array1.Add(r(0).ToString())
array1.Add(r(1).ToString())
array1.Add(r(2).ToString())
array1.Add(r(3).ToString())

The datareader starts at position 0 just like everything else in dotnet.

Carra