Index was outside the bounds of the array C#.net

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.

Code:
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

kwh3856Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChloesDadCommented:
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
0
kwh3856Author Commented:
I tried that but it did not work.  Here is the screen shot.
ssnarray.jpg
0
käµfm³d 👽Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
P.S.

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.
0
kwh3856Author Commented:
Kaufmed,
Thank you.  That makes complete sense.  It is nice to hear from you again.

Thanks
Kenny
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.