Click here to Skip to main content
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 11-Oct-12 0:43am
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 Peter Leow 210
1 Abhinav S 155
2 Mika Wendelius 131
3 Sergey Alexandrovich Kryukov 125
4 TheRealSteveJudge 114
0 Sergey Alexandrovich Kryukov 8,613
1 OriginalGriff 6,536
2 Peter Leow 3,727
3 Zoltán Zörgő 3,456
4 Richard MacCutchan 2,442


Advertise | Privacy | Mobile
Web01 | 2.8.150123.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