Additional information: This Row already belongs to this table.

Asked

Viewed 58 times

2

I am developing a system that searches for information in an excel and does some treatments to save the data in a dbf database. All the treatments I do are on top of Datatables, so I use dataRows and after a certain time, I am facing many problems with the following error "Additional information: This Row already belongs to this table."

I already researched some solutions, but I made a query between two dataTables.

var dtJoin = from dtOri in dbCons.getDataTable("\\ArqDbf.DBF").AsEnumerable()
             join dtFKey in dbCons.getDataTable("\\ArqDbf.DBF").AsEnumerable()
             on dtOri["prnt_ent"] equals dtFKey["org_ent_id"]
             select dtJoinOrg.LoadDataRow(new object[]
             {
                 dtOri["org_ent_id"],
                 dtOri["org_ent_nm"],
                 dtOri["org_en_des"],
                 dtOri["prnt_ent"],
                 dtFKey["org_ent_nm"],
                 dtFKey["org_en_des"]  
             },false);

dtOrgs.TableName = "Orgs";
foreach (DataRow drJoin in dtJoin)
{
    dtJoinOrg.Rows.Add(drJoin);
}

And at the point where I do the "dtJoinOrg.Rows.Add" it gives this error, but where is the dataRow linked to a datatable? How can I solve this problem?

About dataRow belonging to a datatable, I created the following solution:

/*
    Nesse caso o pDtClone tem a mesma estrutura do pDtOri. 
    Caso alguem se pergunte por que estou fazendo isso, 
    é que há um tratamento durante a execução em que poderei alterar as 
    informações do pDtClone e depois vou comparar com o pDtOri
*/
DataRow[] drResult = pDtOri.Select(vWhere);

if (drResult.Length > 0)
{
    for (int i = 0; i < drResult.Length; i++)
    {
        DataRow drNew = pDtClone.NewRow();
        try
        {
            int vColIndex = 0;
            foreach (object vValue in drResult[0].ItemArray)
            {
                drNew[vColIndex] = vValue;
                vColIndex++;
            }
            pDtClone.Rows.Add(drNew);
        }
        finally
        {
            drNew = null;
        }
    }
}

This so far is a solution I have found, but I would like to see other solutions.

1 answer

3


You cannot insert a row more than once in a table.

The DataTable is a database relational table representation, so each row, in addition to the columns that have been defined, there is an internal identifier that makes each row unique, independent of the columns or their values.

In his select is already including the lines using the LoadDataRow(). If you check with Quickwatch you will see that the lines are already there, and are trying to reinsert them. Maybe just save the data with the SaveState() of DataTable.

Finally, I recommend to stop using DataTable. If it is possible to work with Enumerators only, use them with Linq and Lambda expression.

I came to write an article discouraging to use DataTable, you can read the article here.

  • Got it, really... in my case, I could trade most of the dataTables for Lists, thanks for the article and for the reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.