Learner
Learner

Reputation: 1376

How to insert multiple datatable data into one datatable c#

I have a function which will return multiple ItemIDs as

           for (int i = 0; i < dt.Rows.Count; i++)
            {
                ItemID = int.Parse(dt.Rows[i]["item_Id"].ToString());
                dtAtlr = prodctsDCCls.getItemIds(ItemID);
                //dtItems = dtAtlr.Copy();
            }

I want to keep on searching for all ItemIds from the same table and have to save all the data in one datatable.If I copy one datatable to another datatable, that is replacing the previous datatable. but I need all the data. Please anybody help me

Upvotes: 2

Views: 1996

Answers (2)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

You can check DataTable.Merge

Merge the specified DataTable with the current DataTable.

Upvotes: 1

Habib
Habib

Reputation: 223257

Use DataTable.Merge to merge two data tables. So your code would be:

for (int i = 0; i < dt.Rows.Count; i++)
{
    ItemID = int.Parse(dt.Rows[i]["item_Id"].ToString());
    dtAtlr.Merge(prodctsDCCls.getItemIds(ItemID)); // For Merging
}

By using DataTable.Copy, your datatable dtAtlr will have the last returned DataTable against the ItemID

Upvotes: 1

Related Questions