PC Review


Reply
Thread Tools Rate Thread

DataTable.ImportRow bug (no exception)

 
 
adam
Guest
Posts: n/a
 
      30th Jul 2003
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();
}
}


 
Reply With Quote
 
 
 
 
Rick Rainey[MSFT]
Guest
Posts: n/a
 
      30th Jul 2003
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.

 
Reply With Quote
 
Derek LaZard
Guest
Posts: n/a
 
      30th Jul 2003
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


"adam" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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();
> }
> }
>
>

 
Reply With Quote
 
New Member
Join Date: Jul 2010
Posts: 1
 
      7th Jul 2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTable.ImportRow rkeslar Webmaster / Programming 0 12th Jul 2010 04:36 PM
DataTable.ImportRow question kempshall Microsoft ASP .NET 1 7th Jun 2006 08:47 PM
DataTable.ImportRow question kempshall Microsoft Dot NET Framework 1 7th Jun 2006 08:47 PM
datatable.importrow bug hharry Microsoft ADO .NET 1 12th May 2006 09:08 PM
Re: DataTable.ImportRow S. Justin Gengo Microsoft ASP .NET 0 15th Aug 2003 07:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:18 PM.