11 Replies - 513 Views - Last Post: 02 September 2013 - 11:47 AM Rate Topic: -----

#1 Sasori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Error with 'index outside bounds of array'

Posted 01 September 2013 - 08:53 AM

Hi all,

I am having a problem with this program I am trying to write. What I have to do is access a MS Access database, read a table, sort the data and output the top ten most occurring and lowest 10 least occurring numbers in that table. What I have tried to do is put the data in an array, then bubble sort it and output the top and lowest 10 of the array. However, I am having some problems; namely that one line keeps returning an error about the index being outside the bounds of the array... I have tried changing some values, as well as doing a different selection, but it still won't work. Also, my program doesn't output anything (which I am guessing is due to the error). Any help would be greatly appreciated!! Te error is to do with this line;
counts(reader(i)) = counts(reader(i) + 1


Imports System.Data.OleDb

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'establish connection with database
        Dim strConnection As String
        Dim cmd As OleDb.OleDbCommand
        Dim reader As OleDb.OleDbDataReader

        strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\2013\IPT\Term 3\keno.accdb;User ID=Admin;Password=;"

        cmd = New OleDb.OleDbCommand("select * from KenoDraws")
        cmd.Connection = New OleDb.OleDbConnection(strConnection)
        cmd.Connection.Open()
        reader = cmd.ExecuteReader()

    'set up for bubble sort
    Dim counts(80) As Integer
    Dim itemp As Integer 'itemp is simply a 'holder' for the bubble sort; it holds the number before it is swapped
    Dim numfields = reader.FieldCount

        Do While reader.Read() 'reads from the database
            For i = 1 To numfields - 1
                counts(reader(i)) = counts(reader(i) + 1
            Next i
        Loop
    'bubblesort
        For i = 0 To 79
            For j = i + 1 To 79
                If counts(j) < counts(i) Then 'if the number selected is less than the one in the array, then the two are swapped
                    itemp = counts(i)
                    counts(i) = counts(j)
                    counts(j) = itemp
                End If
            Next j
        Next i



        'For K = 0 To 9
        ListBox1.DataSource = counts
        'Next K


        reader.Close()
        cmd.Connection.Close()




    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Error with 'index outside bounds of array'

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,498
  • Joined: 08-April 09

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 10:52 AM

Array indices start at 0, so if you have array {3,1,2} that means your last index is 2, but if you somehow try to access that is > 2 you'll get that error.
Your counts array might be trying to store or read index that is higher that 80, does counts(reader(i) + 1 return value higher that 80?
Was This Post Helpful? 1
  • +
  • -

#3 Sasori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 12:10 PM

View PostMichael26, on 01 September 2013 - 10:52 AM, said:

Array indices start at 0, so if you have array {3,1,2} that means your last index is 2, but if you somehow try to access that is > 2 you'll get that error.
Your counts array might be trying to store or read index that is higher that 80, does counts(reader(i) + 1 return value higher that 80?


Thankyou for your reply, my count array is displaying a value of 81! However, nothing I do can seem to lower it. Do you know what part of the code is causing this?

Thankyou!
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,498
  • Joined: 08-April 09

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 12:40 PM

How about instead of array you use List<T> where T is type(in your case integer)
You connect to database and select statement returns 81 like you said in line 27
  Do While reader.Read() 'reads from the database


List underneath uses array and can dynamically resize, so i think it's better option.

This post has been edited by Michael26: 01 September 2013 - 12:44 PM

Was This Post Helpful? 1
  • +
  • -

#5 Sasori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 12:56 PM

View PostMichael26, on 01 September 2013 - 12:40 PM, said:

How about instead of array you use List<T> where T is type(in your case integer)
You connect to database and select statement returns 81 like you said in line 27
  Do While reader.Read() 'reads from the database


List underneath uses array and can dynamically resize, so i think it's better option.

Again thankyou for your help! I have just looked up lists on the MSDN site, but from what I can gather you have to populate it with a query...?
What my program is trying to do is sift though the table (every column but the first) and find out how many times each number (from 1-80) appears... so there is no table containing the values 1-80. Is there a better way for me to do this?
(Sorry for all my questions)
Was This Post Helpful? 0
  • +
  • -

#6 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,498
  • Joined: 08-April 09

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 01:16 PM

Quote

What I have to do is access a MS Access database, read a table, sort the data and output the top ten most occurring and lowest 10 least occurring numbers in that table. What I have tried to do is put the data in an array, then bubble sort it and output the top and lowest 10 of the array.

Here is a suggestion, you can use sql SELECT TOP clause to select your TOP 10 and sort them in ascending or descending order using ORDER BY ASC or DESC. Use that query to populate your List

This post has been edited by Michael26: 01 September 2013 - 01:17 PM

Was This Post Helpful? 1
  • +
  • -

#7 Sasori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 01:27 PM

View PostMichael26, on 01 September 2013 - 01:16 PM, said:

Quote

What I have to do is access a MS Access database, read a table, sort the data and output the top ten most occurring and lowest 10 least occurring numbers in that table. What I have tried to do is put the data in an array, then bubble sort it and output the top and lowest 10 of the array.

Here is a suggestion, you can use sql SELECT TOP clause to select your TOP 10 and sort them in ascending or descending order using ORDER BY ASC or DESC. Use that query to populate your List

Thankyou very much, you have been a great help!
Was This Post Helpful? 0
  • +
  • -

#8 Sasori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 01:47 PM

OK, I am back again to annoy you... I have tried what you have said using this example from MSDN:
Sub Main()
    ' Create a list of students. 
    Dim students = GetStudents()
    ' Display the names in the list.
    DisplayList(students)
    ' ****Paste query and query execution code from the walkthrough, 
    ' ****or any code of your own, here in Main.
    Console.ReadLine()
End Sub


However it is not working. How would I get this to work with counts? Also, will it work seeing as I have to count all the numbers from 20+ columns?

This post has been edited by andrewsw: 01 September 2013 - 02:52 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#9 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,498
  • Joined: 08-April 09

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 02:04 PM

I think i need more code, what does DisplayList() do?
Was This Post Helpful? 0
  • +
  • -

#10 Sasori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 02:14 PM

View PostMichael26, on 01 September 2013 - 02:04 PM, said:

I think i need more code, what does DisplayList() do?

Here is the entire sample;
Module Module1

    Sub Main()
        ' Create a list of students. 
        Dim students = GetStudents()
        ' Display the names in the list.
        DisplayList(students)
        ' ****Paste query and query execution code from the walkthrough, 
        ' ****or any code of your own, here in Main.
        Console.ReadLine()
    End Sub 

    ' Call DisplayList to see the names of the students in the list. 
    ' You can expand this method to see other student properties. 
    Sub DisplayList(ByVal studentCol As IEnumerable(Of Student))
        For Each st As Student In studentCol
            Console.WriteLine("First Name: " & st.First)
            Console.WriteLine(" Last Name: " & st.Last)
            Console.WriteLine()
        Next 
    End Sub 

    ' Function GetStudents returns a list of Student objects. 
    Function GetStudents() As IEnumerable(Of Student)
        Return New List(Of Student) From
            {
             New Student("Michael", "Tucker", "Junior", 10),
             New Student("Svetlana", "Omelchenko", "Senior", 2),
             New Student("Michiko", "Osada", "Senior", 7),
             New Student("Sven", "Mortensen", "Freshman", 53),
             New Student("Hugo", "Garcia", "Junior", 16),
             New Student("Cesar", "Garcia", "Freshman", 4),
             New Student("Fadi", "Fakhouri", "Senior", 72),
             New Student("Hanying", "Feng", "Senior", 11),
             New Student("Debra", "Garcia", "Junior", 41),
             New Student("Lance", "Tucker", "Junior", 60),
             New Student("Terry", "Adams", "Senior", 6)
            }
    End Function 

    ' Each student has a first name, a last name, a class year, and  
    ' a rank that indicates academic ranking in the student body. 
    Public Class Student
        Public Property First As String 
        Public Property Last As String 
        Public Property Year As String 
        Public Property Rank As Integer 

        Public Sub New()

        End Sub 

        Public Sub New(ByVal firstName As String,
                       ByVal lastName As String,
                       ByVal studentYear As String,
                       ByVal studentRank As Integer)
            First = firstName
            Last = lastName
            Year = studentYear
            Rank = studentRank
        End Sub 
    End Class 
End Module


I have no idea how to implement that to my code...
Was This Post Helpful? 0
  • +
  • -

#11 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,498
  • Joined: 08-April 09

Re: Error with 'index outside bounds of array'

Posted 01 September 2013 - 04:19 PM

My suggestion was that you execute query SELECT TOP 10 from table_name (table name is the name of your table) and you return the result of that query as a List(Of Students)
cmd = New OleDb.OleDbCommand("Run that query here")
'retrieve data using datareader, populate dataTable with data from dataReader and store it to the list(of students)
'I'm writing this from the top of my head, you will test this and adapt to your example.
Private Function MakeStudent(dRow As DataRow) As Student
	Dim FirstName As String = dRow("StudentName").ToString()
        ' "StudentName" is the name of the row, you'll replace this with the name of your column
	Dim LastName As String = dRow("StudentLastName").ToString()
	Dim Rank As String = dRow("StudentPosition").ToString()
	Dim Year As Integer = Integer.Parse(dRow("StudentAge").ToString())

	Return New Student(FirstName , LastName , Rank , Year )
	'This returns one student object.
End Function

Private Function MakeStudent(dt As DataTable) As List(Of Student)
	Dim list As New List(Of Student)()

	For Each row As DataRow In dt.Rows 		
		list.Add(MakeStudent(row)) 'This will populate your list with student object
	Next

	Return list
End Function

'Now you have your student list, you can now display that list, use foreach to loop through that.



It's late i probably have errors in here, i'm off to sleep :)

There are great tutorial here on Collection Basic and In depth
Was This Post Helpful? 0
  • +
  • -

#12 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,498
  • Joined: 08-April 09

Re: Error with 'index outside bounds of array'

Posted 02 September 2013 - 11:47 AM

Sasori did you get it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1