Additional information: This row already belongs to this table.

2

I'm developing a system that looks for information in an excel and does some processing 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'm facing many problems with the following error:

I've researched some solutions, but I've done 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 does the dataRow bind to a dataTable? How can I resolve this problem?

About dataRow belonging to a dataTable, I've created the following workaround:

/*
    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 is a solution I have found so far, but I would like to see other solutions.

    
asked by anonymous 15.08.2016 / 17:14

1 answer

3

You can not insert a row more than once into a table.

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

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

Finally, I recommend stopping using DataTable . If you can only work with enumerators, use only them with Linq and Lambda expression.

I even wrote an article discouraging you from using DataTable , you can read the article here .

    
15.08.2016 / 18:01