Looping through an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Moe Haroon

    Looping through an array

    I have been able to run a query and get the results in an Arraylist. Query returns 2 results
    alist[0] and alist[1]
    //OUTPUT
    DBID,VERSION,DB _NAME,INSTANCE_ NAME,HOST_NAME
    alist[0] == 806497854,10.2. 0.5.0,ALPHA,ALP HA,ALPHA-SERVE alist[1] == 3895980978,10.2 .0.1.0,BETA,BET A,BETA-SERVER
    // END OF OUTPUT

    as you can see from the output, within the alist[0] I have 5 ItemArray objects
    I'm trying to get a checkbox populated with list of items from arraylist. I'm getting "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index" error. here is the code
    Code:
    alist = new ArrayList();
                alist = (ArrayList)ProgramLogic.runsqlAL(sqltext, Form1.OraConn);
                // alist has 2 items 0 and 1
                // alist itemArray has 5 objects in each itemlist
                for ( int i = 0; i <= alist.Count;i++)
                {
                    for (int j = 0; j <= (((DataRow)alist[i]).ItemArray.Count()); j++)
                    {
                        MessageBox.Show("i is " + i.ToString() + "and j is " + j.ToString());
                        //avail_dbid_CB.Items.Add(((DataRow)alist[i]).ItemArray[j].ToString());
                    }
                }
    any help would be greatly appreciated
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    You have to use i < ...Count instead of i <= ...Count. The same for j.

    It's because when Count is 2, you would access array[0], array[1] and array[2] but array[2] doesn't exist.

    Comment

    Working...