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();
}
}
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();
}
}