471,319 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to add rows to DataTable?

I keep getting this error with the following code:
This row already belongs to another table

DataSet ds = new DataSet();
DataTable dtPersonID = new DataTable();
dtPersonID.Columns.Add("PersonID");
foreach (DataRow dr in dt.Rows)
{
dtPersonID.Rows.Add(dr);
}

Why does it matter if two tables have the same row? This is my first
pass through the app so dtPersonID does not have any rows. I'm
transferring the PersonID column from the dt table into dtPersonID.

Thanks,
Brett

Dec 19 '05 #1
5 16799
Brett,

It matters because the row has a Table property which it exposes that
indicates which table it is a part of.

You should be able to pass the row dr to the ImportRow method on the dt
DataTable to import the row (it really copies the row from one table to
another).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I keep getting this error with the following code:
This row already belongs to another table

DataSet ds = new DataSet();
DataTable dtPersonID = new DataTable();
dtPersonID.Columns.Add("PersonID");
foreach (DataRow dr in dt.Rows)
{
dtPersonID.Rows.Add(dr);
}

Why does it matter if two tables have the same row? This is my first
pass through the app so dtPersonID does not have any rows. I'm
transferring the PersonID column from the dt table into dtPersonID.

Thanks,
Brett

Dec 19 '05 #2
Perfect! I just switched the .Add() for the .ImportRow().

How does ImportRow get around the above issue? In other words, what is
the difference between the two methods?

Thanks,
Bret

Dec 19 '05 #3
Bret,

Basically, it calls AddNew on the Rows collection of the new table,
passing the values of the original row to the that method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Perfect! I just switched the .Add() for the .ImportRow().

How does ImportRow get around the above issue? In other words, what is
the difference between the two methods?

Thanks,
Bret

Dec 19 '05 #4
I'd like to write the rows out to XML in a format similar to:
<ROOT><Person PersonId="119323" /><Person PersonId="119324" /></ROOT>

but ds.WriteXML() only gives
<PersonIDSet />

Here's the code used to get the dataset XML

System.IO.StringWriter sw = new System.IO.StringWriter();
XmlTextWriter xmltw = new XmlTextWriter(sw);

ds.WriteXml(xmltw, XmlWriteMode.IgnoreSchema);
string xml = sw.ToString();
Debug.WriteLine(xml);

I wanted to make sure the table rows have values. I'd tried this

for (int i = 0; i <dtPersonID.Rows.Count; i++)
Debug.WriteLine(dtPersonID.Rows[i].ItemArray);

but only get
System.Object[]

However, in the Command Window, I type dtPersonID.Rows[0].ItemArray and
get
{Length=1}
[0]: "57843"

Any ideas?

Thanks,
Brett

Dec 19 '05 #5
I found the problem. That particular DataTable needed to be added to
the DataSet. It is writing now in this format:

<?xml version="1.0" standalone="yes"?>
<PersonIDSet>
<dtPersonID>
<PersonID>57843</PersonID>
</dtPersonID>
<dtPersonID>
<PersonID>58750</PersonID>
</dtPersonID>
</PersonIDSet>

How can I get it to write in this format:
<ROOT>
<PersonID>57843</PersonID>
<PersonID>58750</PersonID>
</ROOT>

Thanks,
Brett

Dec 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by a | last post: by
6 posts views Thread by Pete Wittig | last post: by
9 posts views Thread by Anil Gupte | last post: by
4 posts views Thread by =?Utf-8?B?cmFuZHkxMjAw?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.