This Row Already Belongs to Another Table Error When Trying to Add Rows

This Row already belongs to another table error when trying to add rows?

You need to create a new Row with the values from dr first. A DataRow can only belong to a single DataTable.

You can also use Add which takes an array of values:

myTable.Rows.Add(dr.ItemArray)

Or probably even better:

// This works because the row was added to the original table.
myTable.ImportRow(dr);

// The following won't work. No data will be added or exception thrown.
var drFail = dt.NewRow()
drFail["CustomerID"] = "[Your data here]";
// dt.Rows.Add(row); // Uncomment for import to succeed.
myTable.ImportRow(drFail);

This Row Already Belongs To This Table

The code that finally worked was this:

public static DataTable AddNewAllocations(string pCaseNo, DataTable pTable)
{
try
{
DataTable newTable = NewAllocationTable();

string sqlText = "SELECT UserID FROM tblUsers;";
aSqlQuery aQ = new aSqlQuery(sqlText, "table");
DataTable userTable = aQ.TableResult;

foreach (DataRow userRow in userTable.Rows)
{
int allocAlready = 0;
foreach (DataRow allocRow in pTable.Rows)
{
if (allocRow["FeeEarner"].ToString() == userRow["UserID"].ToString())
{
allocAlready = 1;
}
}

if (allocAlready == 0)
{
string strUser = userRow["UserID"].ToString();
decimal fees = cTimesheet.UserFees(strUser, pCaseNo);
int intCaseNo = Int32.Parse(pCaseNo);
if (fees > 0)
{
Object[] array = new object[8];
array[0] = 0;
array[1] = intCaseNo;
array[2] = DateTime.Today;
array[3] = strUser;
array[4] = fees;
array[5] = 0;
array[6] = fees;
array[7] = true;
newTable.Rows.Add(array);
}
}
}

foreach (DataRow row in pTable.Rows)
{
newTable.ImportRow(row);
}

newTable.DefaultView.Sort = "AllocID";
return newTable;
}

catch (Exception eX)
{
throw new Exception("cAllocation: Error in NewAllocations()" + Environment.NewLine + eX.Message);
}
}

I think the key was using ImportRow rather than Rows.Add. I still use Rows.Add in my method but only when adding rows to a newly created table. I then loop through the existing table which was passed in as a paramater, and use ImportRow to add each row of the paramater table, to the newly created one. I then pass the new combined table out in my return statement, rather than a modified parameter table.

This row already belongs to this table even with new row inside loop

Look at your code:

Dim dsRow As System.Data.DataRow = dsTemp.Tables(0).Rows(index)
'...
If Not Microsoft.VisualBasic.IsDBNull(value) AndAlso String.Compare(value.ToString, "ColDes", True) = 0 Then
Dim newRow As System.Data.DataRow = dsTemp.Tables(0).NewRow
dsTemp.Tables(0).Rows.Remove(dsRow)
dsTemp.Tables(0).Rows.InsertAt(dsRow, 1)
dsTemp.Tables(0).NewRow()
dsTemp.Tables(0).Rows.InsertAt(dsRow, 3)
End If

What is that code doing? Look at each line and actually ask yourself what it does.

  1. First you get an existing row form the table and assign it to the
    dsRow variable.
  2. Inside the If block, you create a new row and assign it the
    newRow variable. You never use that variable again, so what was the point of that?
  3. You then remove the existing row from the table.
  4. You then insert the existing row as the second row in the table.
  5. You then create a new row and do nothing with it at all, so what was the point of that?
  6. You then try to insert the existing row a second time, as the fourth row in the table.

Is it really unexpected that the error message is telling you that that row already belongs to the table?

For one thing, when you call NewRow, that simply creates a new row. That row is NOT added to the table automatically. How could it be? It's empty and the table may have non-nullable columns. You are supposed to populate it as required and then add it yourself.

Apart from that, you can't add or insert the same row twice.

If all you're trying to do is insert blank rows then you need to actually insert blank rows, not existing rows that you removed. You need to twice call NewRow followed by Insert, e.g.

myTable.Rows.Insert(myTable.NewRow(), 1)
myTable.Rows.Insert(myTable.NewRow(), 3)

c# adding row that already belongs to a datatable

To be honest I don't 100% understand your question, however to fix the exception:

The row is already belongs to another datatable.

Change:

oNewTable.Rows.Add(oRow);

To:

oNewTable.ImportRow(oRow);

Alternatively create a new row and clone the ItemArray.

foreach (var oRow in cRows.Values)
{
var newRow = oNewTable.NewRow();
newRow.ItemArray = oRow.ItemArray.Clone() as object[];
oNewTable.Rows.Add(newRow);
}

DataTable Manual Rows Insert: This row already belong to this table

When you do datatable.NewRow(), it is created in datatable with default values for all the columns in it. It returns the newly created object of datarow and you need to populate column with appropriate values.

The issue here is you have datatable.NewRow() statement outside of for loop and datatable.Row.Add is called in for loop.
So basically you are trying to add the same row multiple times.

You need to move datatable.NewRow() statement inside inner for loop which as following.

 for (int j = 0; j < Convert.ToInt32(strWorking); j++)
{
var dr = dttempEmployee.NewRow();

// Other logic

dttempEmployee.Rows.Add(dr);
}


Related Topics



Leave a reply



Submit