2

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

5
  • 1
    It's not clear from your question what you have tried, what was the problem(was there an exception?) or what's the desired result. Provide a short(but complete) sample, you can replace your getItemIds with a foo-method that returns a DataTable. Commented Feb 19, 2013 at 10:13
  • Side-note: If item_Id is already an int use the strongly typed DataRow extension method Field(which also supports nullable types): item_Id = dt.Rows[i].Field<int>("item_Id"); Commented Feb 19, 2013 at 10:17
  • that is replacing the previous datatable. this is my problem sir Commented Feb 19, 2013 at 10:18
  • Iam doing dtItems=dtAtlr.Copy(); so dat it is replacing the previous data with the new data.. :) Commented Feb 19, 2013 at 10:21
  • Thanks you sir.. I tried dt.Clone and tried to copytodatatable method too with a foreach loop Commented Feb 19, 2013 at 10:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

You can check DataTable.Merge

Merge the specified DataTable with the current DataTable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.