[RESOLVED] Tables and ImportRow?
Can anyone see what I'm doing wrong I'm getting an ArgumentNullException: Argument column can't be null ?
Code:
...
DataSet ds = testDSet( ...);
result.Tables.Add( ds.Tables[0].Copy() );
result.Tables.Add( ds.Tables[1].Copy() );
//here's the code that generates the error!
result.Relations.Add( result.Tables[0].Columns["CustomerNumber"] , result.Tables[1].Columns["CustomerNumber"] );
...
private static DataSet testDSet(...)
{
...
ds.Tables[0].TableName = "test";
...
ds.Tables.Add("Products");
...
DataSet dsProducts = GetProducts(...)
foreach( DataRow dr2 in dsProducts.Tables[0].Rows )
{
ds.Tables["Products"].ImportRow( dr2 );
}
return ds;
}
Re: Tables and ImportRow?
My guess would be that in one of those tables you have a dbnull value in the column you are attempting to add the relation on.
Re: Tables and ImportRow?
Code samples should always meet the minimal yet complete criteria. Yours does not. This makes it impossible to conclusively determine the cause of the error.
For example, you would get this error if either of the tables does not have a column EXACTLY called "CustomerNumber". This is probably the most likely reason. jshultz's post may also be the reason, and there are other potential reasons.
Re: Tables and ImportRow?
Here is the right solution! =)
Code:
...
DataSet ds = testDSet( ...);
result.Tables.Add( ds.Tables[0].Copy() );
result.Tables.Add( ds.Tables[1].Copy() );
DataRelation dr = new DataRelation( "Customernumber" , result.Tables[1].Columns["CustomerNumber"] , result.Tables[2].Columns["CustomerNumber"] );
dr.Nested = true;
result.Relations.Add( dr );
...
private static DataSet testDSet(...)
{
...
ds.Tables[0].TableName = "test";
...
ds.Tables.Add("Products");
...
DataSet dsProducts = GetProducts(...)
int i = 0;
foreach( DataRow dr2 in dsProducts.Tables[0].Rows )
{
if( i == 0 )
{
ds.Tables.Add( dsProducts.Tables[0].Clone() );
}
foreach( DataRow dr2 in dsProducts.Tables[0].Rows )
{
ds.Tables["Products"].ImportRow( dr2 );
}
return ds;
}