Click here to Skip to main content
11,718,589 members (84,276 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hi,

What's the wrong in this code , i want to store dt1 data dt2 data is not match that information i want to store in another datatable dt3.But in the below highlighted line it shows the error this row already belongs to another table.
DataTable dt3 = dt1.Clone();
foreach (DataRow dr1 in dt2.Rows)
{
    foreach (DataRow dr2 in dt1.Rows)
    {
        if (dr1.ItemArray[0].ToString() != dr2.ItemArray[0].ToString())
        {
            DataRow dr3 = dt3.NewRow();
            dr3 = dr2;
            dt3.Rows.Add(dr3);
        }
    }
}

can any one help me to resolve this issue....

Thanx in advance....
Posted 10-Oct-12 23:43pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

A Row can only be in one table at a time. If you want the same data in more than one table, you will have to copy the data into a new DataRow and add the new row instead.

It's just like filing a letter - the original can only be in one place: if you want it in two separate physical files, you have to use a photocopier.
  Permalink  
Comments
OriginalGriff at 11-Oct-12 6:21am
   
DataRow dr3 = dt3.NewRow();
dr3 = dr2;
dt3.Rows.Add(dr3);
dr3 = dr2;
Copies the reference, not the content.
Think of it as a house address: if you copy the address of your parents house from one piece of paper to another, you do not copy the house itself!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use importrow

DataTable dt3 = dt1.Clone();
                    foreach (DataRow dr1 in dt2.Rows)
                    {
                        foreach (DataRow dr2 in dt1.Rows)
                        {
                            if (dr1.ItemArray[0].ToString() != dr2.ItemArray[0].ToString())
                            {
                                DataRow dr3 = dt3.NewRow();                               
                                dt3.ImportRow(dr2);                                
                            }
                        }
                    }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You have to use ImportRow instead of Rows.Add.
dt3.ImportRow(dr3);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You have to use ImportRow instead of Rows.Add
or
Use [datatable].Rows.Add([datarow].ItemArray);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

DataTable First;//This Data Table contains the data

Now i want to copy the row to another table, while coping i got the error message

"This row already belongs to another table."

To fix the above siuue i have used the below solution.

DataTable second= new DataTable();
second = First.Clone();
//With out clone the First table, second table wont fill the data
// even it execute the below ststement table doesn't contains the data.
dt.ImportRow(msg);

Thank you,
Ravindra
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 651
1 OriginalGriff 210
2 F-ES Sitecore 185
3 Jochen Arndt 160
4 Maciej Los 130
0 OriginalGriff 535
1 Maciej Los 529
2 Richard MacCutchan 440
3 CHill60 415
4 F-ES Sitecore 415


Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100