Solved

"DataTable already belongs to this DataSet.

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

12 Experts available now in Live!

Get 1:1 Help Now