Solved

"DataTable already belongs to this DataSet.

Posted on 2012-03-23
2
716 Views
Last Modified: 2012-03-23
HI,

when I add records to a datatable, I get the following error.

{"DataTable already belongs to this DataSet."}

code
 da.Fill(ds);
            //dt = ds.Tables[0];

            DataTable dt;
            dt = new DataTable("Sample");
            dt.Columns.Add("Id", typeof(integer));
            dt.Columns.Add("FName", typeof(string));
            dt.Columns.Add("LName", typeof(string));

            DataRow newDataRow;

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                newDataRow = dt.NewRow();
                newDataRow["Id"] = dr["Id"].ToString();
                newDataRow["FName"] = dr["FName"].ToString();
                newDataRow["LName"] = dr["LName"].ToString();
                dt.AcceptChanges();
                Tables.Add(dt); <--- error here
            }

How can I fix the problem?

please help.

ayha
0
Comment
Question by:ayha1999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 37756032
Take this line:
Tables.Add(dt); <--- error here
out of the foreach loop.
If you read the error message you shoiuld notice it is telling you the table is being added more than once
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 37756044
Thank you so much.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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