Index was outside the bounds of the array

Posted on 2014-07-13
Last Modified: 2014-07-13
I am getting an Index was outside the bounds of the array.  I am not sure why this error is occurring.  All I am trying to do is loop through the contents of a grid and pick up the SSN field from each row.  If there are 3 rows, I should have 3 records in my array for ssn.  If there are 100 rows I should have 100 records in my array data.  Any help is greatly appreciated.

ContainerGridRecordCollection gridRows = WebHierarchicalDataGrid1.GridView.Rows;

            int rowsCount = gridRows.Count;

            int columnsCount = WebHierarchicalDataGrid1.GridView.Columns.Count;

            for (int r = 0; r < rowsCount; r++)

                for (int c = 0; c < columnsCount; c++)

                    string[] ssnArray = new string[] { };

                    String cellValue = gridRows[r].Items[1].Value.ToString();

                    ssnArray[r] = cellValue;

                }    <------------------ Error Occurs Here

Open in new window

Question by:kwh3856
    LVL 15

    Expert Comment

    you do have 100 elements in the array, 0 to 99, so you have to subtract 1 when accessing the array elements, ie (r-1) in your code

    Author Comment

    I tried that but it did not work.  Here is the screen shot.
    LVL 74

    Accepted Solution

    You have two problems:

    First, you have declared the array ssnArray local to to the inner-most for loop. It won't save any values between iterations. You need to widen the scope of this variable.

    Second, you declared this same array with no space. You need to tell the compiler how many elements you expect to have in the array. It should be the same size as the number of rows, so:

     string[] ssnArray = new string[rowsCount];

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽

    When I say "widen the scope" of the variable, it really needs to be declared outside of all of the loops, or at least the one that loops over the rows.

    Author Closing Comment

    Thank you.  That makes complete sense.  It is nice to hear from you again.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now