Index was outside the bounds of the array

i have a datatable called onlynewwithstatuschange
 DataTable onlynewwithstatuschange = new DataTable();
            onlynewwithstatuschange.Columns.Add("CUSTOMER", typeof(string));
            onlynewwithstatuschange.Columns.Add("NAME", typeof(string));
            onlynewwithstatuschange.Columns.Add("ADDRESS", typeof(string));
            onlynewwithstatuschange.Columns.Add("ADDRESS 2", typeof(string));
            onlynewwithstatuschange.Columns.Add("CITY", typeof(string));
            onlynewwithstatuschange.Columns.Add("STATE", typeof(string));
            onlynewwithstatuschange.Columns.Add("ZIP CODE", typeof(string));
            onlynewwithstatuschange.Columns.Add("PARENT", typeof(string));
            onlynewwithstatuschange.Columns.Add("TELEPHONE", typeof(string));
            onlynewwithstatuschange.Columns.Add("FAX", typeof(string));
            onlynewwithstatuschange.Columns.Add("TERMS OF PAYMENT", typeof(string));
            onlynewwithstatuschange.Columns.Add("COUNTRY", typeof(string));
            onlynewwithstatuschange.Columns.Add("CURRENCY", typeof(string));
            onlynewwithstatuschange.Columns.Add("STATUS", typeof(string));
            onlynewwithstatuschange.Columns.Add("STATUS CHANGED", typeof(string));
            onlynewwithstatuschange.Columns.Add("LONG", typeof(string));
            onlynewwithstatuschange.Columns.Add("LAT", typeof(string));
            onlynewwithstatuschange.Columns.Add("TAXID", typeof(string));

Open in new window


i am doing a status change check earlier in the code and adding those values to another datatable called tempchange.

then running the following code:
foreach (DataRow temp in tempchange.Rows)
            {
                DataRow[] foundRows = c.Select("CUSTOMER = '" + temp["CUSTOMER"].ToString().Trim() + "'");
                if (foundRows.Length != 0)
                {
                    onlynewwithstatuschange.Rows.Add(foundRows[0].ToString(), foundRows[1].ToString().Trim(), foundRows[2].ToString().Trim(), foundRows[3].ToString().Trim(), foundRows[4].ToString().Trim(), foundRows[5].ToString().Trim(), foundRows[6].ToString().Trim(), foundRows[7].ToString().Trim(), foundRows[8].ToString().Trim(), foundRows[9].ToString().Trim(), foundRows[10].ToString().Trim(), foundRows[11].ToString().Trim(), foundRows[12].ToString().Trim(), foundRows[13].ToString().Trim(),[b] temp[1].ToString()[/b] - getting from the temp change table so the use can see the status was changed from what, foundRows[14].ToString().Trim(), foundRows[15].ToString().Trim(), foundRows[16].ToString().Trim());
          }
            }

Open in new window


i counted the array and there are 17 values present and including the temp[1] it comes to a total of 18 values and the onlynewstatuschange datatable  has 18 columns so why then am i getting the index was outside the bounds of the array error?
Aravind RanganathanWindows Application DeveloperAsked:
Who is Participating?
 
Snarf0001Connect With a Mentor Commented:
I think you need another indexer in there.  Hart to tell without the full code, but by calling "foundRows[0], foundRows[1]", you're basically adding the DATAROWs as column values:

like: onlynewwithstatuschange.Rows.Add(firstEntireRow, secondEntireRow, thirdEntireRow etc...)

I think you want something like this:

foreach (DataRow temp in tempchange.Rows)
            {
                DataRow[] foundRows = c.Select("CUSTOMER = '" + temp["CUSTOMER"].ToString().Trim() + "'");
                if (foundRows.Length != 0)
                {
                    onlynewwithstatuschange.Rows.Add(foundRows[0][0].ToString(), foundRows[0][1].ToString().Trim(), foundRows[0][2].ToString().Trim(), foundRows[0][3].ToString().Trim(), foundRows[0][4].ToString().Trim(), foundRows[0][5].ToString().Trim(), foundRows[0][6].ToString().Trim(), foundRows[0][7].ToString().Trim(), foundRows[0][8].ToString().Trim(), foundRows[0][9].ToString().Trim(), foundRows[0][10].ToString().Trim(), foundRows[0][11].ToString().Trim(), foundRows[0][12].ToString().Trim(), foundRows[0][13].ToString().Trim(),[b] temp[1].ToString()[/b] - getting from the temp change table so the use can see the status was changed from what, foundRows[0][14].ToString().Trim(), foundRows[0][15].ToString().Trim(), foundRows[0][16].ToString().Trim());
          }
            }

Open in new window

0
 
Snarf0001Connect With a Mentor Commented:
Note that if your schema is the same, you can just call .ImportRow.
In either case, you need to be looping through the "foundRows" collection as well.
0
 
Aravind RanganathanWindows Application DeveloperAuthor Commented:
Thanks mate i didnt know it needed another indexer
0
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.

All Courses

From novice to tech pro — start learning today.