Solved

Copying a DataRow from one DataTable to another DataTable

Posted on 2004-04-07
4
1,659 Views
Last Modified: 2011-09-20
OK, I'm trying to copy a DataRow from one DataTable to another DataTable.  I throw the exception: "This row already belongs to another table."  I, of course, yell at the machine, "Please make a copy of that row for me!"

      DataTable goose = new DataTable("Goose");
      dtWK = csv.ParseCSV(sFilePathName);
      DataRow dr1 = goose.Rows[5];

      DataTable duck = new DataTable("Duck");
      DataRow dr2 = duck.NewRow();
      dr2 = dr1;
      duck.Rows.Add(dr2);

I can accomplish what I want by looping thru each DataRow.ItemArray, but there must be a copy constructor somewhere, I tried DataRow.Clone(); but that doesn't seem to apply to the whole array.

      DataRow dr2 = duck.NewRow();
      int i = 0;
      foreach (object o in dr1.ItemArray)
           dr2[i++] = o.ToString();
      duck.Rows.Add(dr2);

Thanx, Jim in Vermont
0
Comment
Question by:SparrowVideo
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 250 total points
ID: 10778844
use this

DataTable goose = new DataTable("Goose");
     dtWK = csv.ParseCSV(sFilePathName);
     DataRow dr1 = goose.Rows[5];

     DataTable duck = new DataTable("Duck");
     duck.Rows.Add(dr1.ItemArray);

B..M
0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10778892
As you might have guessed, this error message is because any datarow always "belongs" to a datatable, and cannot exist independently. So once added to a table, the same row cannot be added again to another table.

You can actually use the following stmt for what you want to do:

duck.Rows.Add(dr1.ItemArray)

HTH,
Karun.
0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10778911
Ah, forget it... I'll never learn to refresh before posting...
0
 

Expert Comment

by:thulsey
ID: 11164759
I had same problem.  Thanks for mention of .ItemArray.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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