INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Can't add DataRow to DataTable

Can't add DataRow to DataTable

(OP)
Hello,

I'm having trouble adding a DataRow to a DataTable.

I create the DataRow like so:

DataRow row = myDataTable.NewRow();

I add things to the row, then I try to add the row back to the table:

myDataTable.Rows.Add(row);

It tells me: "This row already belongs to this table."

Ironically, if I try:

row.AcceptChanges();

it tells me: "Cannot perform this operation on a row not in the table."

so is it in the table or is it not?

myDataTable.AcceptChanges() seems to run without any exceptions, but my table is still empty.

What gives?

RE: Can't add DataRow to DataTable

Do you have a primary key set on the table? If you do, you could have a duplicate.

I'm using this in one of my standard libraries without any problems. .NET 2.0.

CODE --> C#

DataRow tempRow;
while (DbDataReader.Read())
{
    tempRow = tempTable.NewRow();
    for (int i = 0; i < DbDataReader.FieldCount; i++)
        tempRow[i] = DbDataReader[i];
    tempTable.Rows.Add(tempRow);
}
return tempTable; 

You could always try this instead when adding the row to the table:

CODE --> C#

myDataTable.ImportRow(row); 

and see if that works.

RE: Can't add DataRow to DataTable

(OP)
Thanks Moregelen,

I solved the problem by adding the row right after creating it.

At first, I had this:

DataRow row = myDataTable.NewRow();
row[1] = x;
row[2] = y;
row[3] = z;
myDataTable.Rows.Add(row);

But now I have this:

DataRow row = myDataTable.NewRow();
myDataTable.Rows.Add(row);
row[1] = x;
row[2] = y;
row[3] = z;

The latter seems to work.

RE: Can't add DataRow to DataTable

Very strange.

RE: Can't add DataRow to DataTable

Solution 1:
<code>
DataRow row = dt.NewRow();
row[0] = "maria";
row[1] = "george";
dt.Rows.Add(row);
// row = dt.NewRow();
row[0] = "Helene";
row[1] = "Maxim";
dt.Rows.Add(row);
</code>
Running the above you get ""This row already belongs to this table."
Uncomment the comments and the second row is added.
Reason: In fact
<cde>
row[0] = "Helene";
row[1] = "Maxim";
</code>
modify the previous values of the same "row" object.

Solution 2:
<code>
DataRow row = dt.NewRow();
row[0] = "maria";
row[1] = "george";
dt.Rows.Add(row); // or dt.ImportRow(row);
row[0] = "Helene";
row[1] = "Maxim";
dt.ImportRow(row);

row[0] = "Christina";
row[1] = "Bill";
dt.ImportRow(row);
</code>
Explanation:
Rows.Add(dr) - add a reference of te dr DataRow object
Rows.ImportRow(dr) - add a copy of the dr DataRow object

obislavu




Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close