Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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

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.
 
Share this answer
 
Comments
[no name] 11-Oct-12 5:51am    
in my dt1 i have ----1,2,3 values
dt2 i have ----1,2
now i want to compare 2 datatables and missmatched values to store in some other datatable, how to do this
[no name] 11-Oct-12 5:53am    
here i'm creating new row and that row assigned to datatable what's the wrong
OriginalGriff 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!
[no name] 11-Oct-12 6:34am    
thanks griff i got the solution dt.importrow(dr);
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);                                
                            }
                        }
                    }
 
Share this answer
 
v2
You have to use ImportRow instead of Rows.Add.
dt3.ImportRow(dr3);
 
Share this answer
 
You have to use ImportRow instead of Rows.Add
or
Use [datatable].Rows.Add([datarow].ItemArray);
 
Share this answer
 
v2
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900