473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Colu mns.Add("Person ID");
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 16909
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.co m

"Brett Romero" <ac*****@cygen. com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.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.Colu mns.Add("Person ID");
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.co m

"Brett Romero" <ac*****@cygen. com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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="11932 3" /><Person PersonId="11932 4" /></ROOT>

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

Here's the code used to get the dataset XML

System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
XmlTextWriter xmltw = new XmlTextWriter(s w);

ds.WriteXml(xml tw, XmlWriteMode.Ig noreSchema);
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.Row s.Count; i++)
Debug.WriteLine (dtPersonID.Row s[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
2199
by: Eva | last post by:
Hi i am trying to insert a new row into one of my datatabels that i have in my dataset when a button is clicked. here is my code Dim ClientInsRow As DataRow = dtClient.NewRo ClientInsRow("Surname") = txtSurname.Tex ClientInsRow("Forename") = txtForename.Tex ClientInsRow("OrgName") = txtOrganisation.Tex ClientInsRow("Address") =...
5
2237
by: randy | last post by:
Hello all, I have a DataTable which I am building column by column and adding rows after each new column. The DataTable columns match the columns in my database table. I'm building the DataTable first and I then want to roll through the DataTable while in memory checking for errors and then commit the rows to my database table (btw this...
3
12577
by: a | last post by:
Hi: I'm trying to add rows to a datatable from an array, but due to lack of brain power, am unable to make this work. I'm being told that I don't have enough columns (I only want one column at this point.) Anyone have any ideas? Thanks ==================================================== private void button1_Click(object sender,...
3
5382
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some example first:
3
32251
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate datatable without having to loop through each column in the datagridview? Or is there a way to retrieve the rows from the original datatable...
6
28296
by: Pete Wittig | last post by:
Hi, I have a DataTable and I want to get a subset of the rows within it. I use the Select method to get my subset and the results are in a DataRow. I want to put those Rows back into a DataTable. I've tried a few variations of the following but no rows are every imported into the datatable: DataRow datrow = ds.Tables.Select("PersonID =...
9
4000
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New DataTable("SliceInfo") Dim tblSliceRatings As New DataTable("SliceRatings") '.... All the adding datacolumns, datarows, etc. goes here.. ...
11
35382
by: inpuarg | last post by:
I have 2 datatables. They are identical. I want to compare them by cell's content. They are all same. But dt1 == dt2 or dt1.GetHashCode() == dt2.GetHashCode() doesn 't work. There are big amount of rows in theese datatables . So i don 't want to enumerate each rows. This is not efficient and unacceptable for my current application.
6
2343
by: Paulers | last post by:
Hello, I have a string that I am trying to add each char to a datatable row. for example if I have a string that looks like "abcdefg", I would like to break it up into an array of characters so I can do this: myDataTable.Rows.Add(array()) instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
4
25976
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I'm working in Visual Studio 2005 and C#. I have a DataTable with many rows. The user can delete a row. After the row is deleted, I'd like to column values for the deleted row, but any attempt to access column values results in the following exception: {"Deleted row information cannot be accessed through the row."} My understanding is...
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7491
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1068
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.