Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2014-07-13
5
Medium Priority
?
5,322 Views
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.

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

0
Comment
Question by:kwh3856
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40193407
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
 

Author Comment

by:kwh3856
ID: 40193409
I tried that but it did not work.  Here is the screen shot.
ssnarray.jpg
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40193464
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40193471
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
 

Author Closing Comment

by:kwh3856
ID: 40193488
Kaufmed,
Thank you.  That makes complete sense.  It is nice to hear from you again.

Thanks
Kenny
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

581 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