473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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(dataRow2, 5)

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

Thanks,
Shawn
Nov 19 '05 #1
3 11431
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(dataRow2, 5)

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

Thanks,
Shawn


Try dataTable2.ImportRow 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_SPAM_4_MEyahoo.com> wrote in message
news:#W**************@tk2msftngp13.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(dataRow2, 5)

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

Thanks,
Shawn


Try dataTable2.ImportRow 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********@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP09.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_SPAM_4_MEyahoo.com> wrote in message
news:#W**************@tk2msftngp13.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(dataRow2, 5)
>
> The line above gives me this error: System.ArgumentException: This row
> already belongs to another table.
> What is the right way to do this?
>
> Thanks,
> Shawn
>
>


Try dataTable2.ImportRow 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
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...
3
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 .... "...
0
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,...
2
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
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...
1
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...
5
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...
6
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.