Solved

"DataTable already belongs to this DataSet.

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now