473,803 Members | 3,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy a DataRow from one DataTable to another

Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains a
DataRow that I need to insert at index 5 in dataTable2. Here is my code:

For Each dataRow1 In dataTable1.Rows
If dataRow1.Item(" id") = "3353" Then
dataRow2 = dataRow1
End If
Next
dataTable2.Rows .InsertAt(dataR ow2, 5)

The line above gives me this error: System.Argument Exception: This row
already belongs to another table.
What is the right way to do this?

Thanks,
Shawn
Nov 19 '05 #1
3 11458
Shawn wrote:
Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains a
DataRow that I need to insert at index 5 in dataTable2. Here is my code:

For Each dataRow1 In dataTable1.Rows
If dataRow1.Item(" id") = "3353" Then
dataRow2 = dataRow1
End If
Next
dataTable2.Rows .InsertAt(dataR ow2, 5)

The line above gives me this error: System.Argument Exception: This row
already belongs to another table.
What is the right way to do this?

Thanks,
Shawn


Try dataTable2.Impo rtRow method instead...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 19 '05 #2
Yes, but what do I have to do to get the new DataRow into index 5 in the
DataTable?

Shawn
"Craig Deelsnyder" <cdeelsny@NO_SP AM_4_MEyahoo.co m> wrote in message
news:#W******** ******@tk2msftn gp13.phx.gbl...
Shawn wrote:
Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains a DataRow that I need to insert at index 5 in dataTable2. Here is my code:
For Each dataRow1 In dataTable1.Rows
If dataRow1.Item(" id") = "3353" Then
dataRow2 = dataRow1
End If
Next
dataTable2.Rows .InsertAt(dataR ow2, 5)

The line above gives me this error: System.Argument Exception: This row
already belongs to another table.
What is the right way to do this?

Thanks,
Shawn


Try dataTable2.Impo rtRow method instead...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 19 '05 #3
Why bother? You can use the .Select method to return an array of DataRows
in any order you want. At the very least, you could add a dummy column to
your
table at runtime as a column to sort on and populate based on your business
logic.

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"Shawn" <bo********@hot mail.com> wrote in message
news:e9******** ******@TK2MSFTN GP09.phx.gbl...
Yes, but what do I have to do to get the new DataRow into index 5 in the
DataTable?

Shawn
"Craig Deelsnyder" <cdeelsny@NO_SP AM_4_MEyahoo.co m> wrote in message
news:#W******** ******@tk2msftn gp13.phx.gbl...
Shawn wrote:
> Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains a > DataRow that I need to insert at index 5 in dataTable2. Here is my code: >
> For Each dataRow1 In dataTable1.Rows
> If dataRow1.Item(" id") = "3353" Then
> dataRow2 = dataRow1
> End If
> Next
>
>
> dataTable2.Rows .InsertAt(dataR ow2, 5)
>
> The line above gives me this error: System.Argument Exception: This row
> already belongs to another table.
> What is the right way to do this?
>
> Thanks,
> Shawn
>
>


Try dataTable2.Impo rtRow method instead...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5833
by: Kelvin | last post by:
Hi All, Due to can't insert new row between row and row while current DataTable looping. In order to solve the problem, I need to clone it as new DataTable, while the DataTable Looping, it also insert both of new row and existing datarow into new DataTable, but I don't know how to do that, Here is my souce code, Please advise. if ( myTable.Rows.Count > 0 )
3
9450
by: ypul | last post by:
can anyone tell me please .. how to copy one datarow from one datatable to another.. I want to extract some specific rows from one datatable and put into antoher .... " datatableOne.rows.add(datatableTwo.rows(i))" this statement does not work ..it says
0
1243
by: Bennett Haselton | last post by:
If I have just filled a DataTable in a typed DataSet with a single row, is there a way I can make a copy of that row, so that I can clear the DataTable in that DataSet and use it for another query, but my copy of the row will be preserved? If I try to do something like this: this.oleDbWbuserAdapter.SelectCommand.CommandText = "SELECT * FROM wbuser where username='bennett';"; this.oleDbWbuserAdapter.Fill(ds1.wbuser);
2
1733
by: Li Pang | last post by:
Hi, I created a datatable (dt1) of 3 columns A, B, C, and I want to copy this datatable into another datatable (dt2) but without the column B. How to do this? Thanks in advance
6
1639
by: JIM.H. | last post by:
Hello I have; DataRow dr= dataSet11.myTable.NewRow(); And I am filling the fields of this datarow. Now I need to create a copy of this row as drCopy and change a few fields and add both to the dataset, how can I do this? Thanks,
1
4058
by: Mo | last post by:
Hi, I have two datasets on two databases one remote and one local. I am trying to copy the local data into the remote. Both tables have the same structure. I use the following DataTable Remote_Table = new DataTable(); Remote_Table = dsRemote.Tables.Clone(); DataRow DR_Local = dsLocal.Tables.Select(); foreach(DataRow Local_Data in(DR_Local))
5
8364
by: samoore33 | last post by:
I use the code below to return rows matching the state in the theState variable. I want to know if it is possible to search through the DataRow that I am returning with the search. I understand that that creates a DataRow Array. I searched through group postings and found where Cor had said something about bulding the DataTable back, not sure what that meant of if I could use it with this, but any help would be appreciated. Dim t As...
6
28331
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 = " + id); DataTable dt = new DataTable();
1
1507
by: SunshineInTheRain | last post by:
Dim dtUn As DataTable dtUn= New DataTable dtUn = BindICUN() Dim i As Integer Dim irow As DataRow Dim mailBody As String = "" Dim myRow As DataRow Dim rowSet As DataRow() rowSet = dtUn.Select("Distinct Member_id is not null")
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10309
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6840
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.