Solved

Copying a DataRow from one DataTable to another DataTable

Posted on 2004-04-07
4
1,650 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
Comment Utility
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
Comment Utility
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
Comment Utility
Ah, forget it... I'll never learn to refresh before posting...
0
 

Expert Comment

by:thulsey
Comment Utility
I had same problem.  Thanks for mention of .ItemArray.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now