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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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

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
Snarf0001Commented:
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
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
arrays

From novice to tech pro — start learning today.