Solved

"DataTable already belongs to this DataSet.

Posted on 2012-03-23
2
696 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

805 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