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.
Got it, really... in my case, I could trade most of the
dataTables
forLists
, thanks for the article and for the reply– Willian