DataTable.ImportRow bug (no exception)

A

adam

the following code demonstrates that DataTable.ImportRow will silently fail
if the row beign imported is detatched. As this has caused me a small
amount of pain I thought I'd mention it.

I my view if the row is not going to be imported an exception should be
thrown.

adam

using System;
using System.Data;
class Class1
{
// one or more arguments to do the bad test
static void Main(string[] args)
{
bool bTestGood = (args.Length==0);
Console.WriteLine("TestGood = " + bTestGood);
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("c1",typeof(string));
dt.Columns.Add(dc);
DataTable dt2 = dt.Clone();
DataRow dr = dt.NewRow();
//dr.RowState == DataRowStates.Detatched
//dt.Rows.Count == 0
//dt2.Rows.Count == 0
if (bTestGood)
{
// add to rows collection
dt.Rows.Add(dr);
//dr.RowState == DataRowStates.Added
//dt.Rows.Count == 1
dt2.ImportRow(dr);
//dt2.Rows.Count == 1
}
else
{
// this will silently fail to import as the row dr is detatched.
dt2.ImportRow(dr); // should go BANG!
//dt2.Rows.Count == 0
}
Console.WriteLine("dr.RowState = " + dr.RowState);
Console.WriteLine("dt.Rows.Count = " + dt.Rows.Count);
Console.WriteLine("dt2.Rows.Count = " + dt2.Rows.Count);
Console.Read();
}
}
 
R

Rick Rainey[MSFT]

Hi Adam ,

Thank you for reporting this issue.

The ImportRow method does not change the RowState property of the row.

From the online help...

"Calling ImportRow preserves the existing DataRowState, along with other values in the row."

Since the row is in a "detached" state, adding it to a table via ImportRow would put the row in a conflicting state; added to a collection but still in a detached
state.

This issue has been brought up before and is being handled as a documentation issue so that future documentation will be more clear on this.


Sincerely,

Rick[MSFT]
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
D

Derek LaZard

Hi Adam. This has not been defined as an exception; I think .ImportRow( dr)

1) adds a .NewRow( dr)
2) and copies state information to the new row: the imported row in the target dataTable has the same state as the row in the source dataTable.

I'm finding that a "detached" DataRow can still be linked to a table (see table data in the debugger)--even if it is not visible or even removed from the DataRowCollection after .AcceptChanges()...

[Maybe this maintains flexibility??...] :)

Derek LaZard
 
Joined
Jul 7, 2010
Messages
1
Reaction score
0
Solution to this bug

Just call AcceptChanges() method before calling ImportRow method
this will fix this issue

Sample Code:

For Each typeDataRow As DataRow In dataRows
typeDataRow.AcceptChanges()
newDataTable.ImportRow(typeDataRow)
Next

Cheers
-Mathiev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top