Click to See Complete Forum and Search --> : [RESOLVED] Tables and ImportRow?


Visslan
February 1st, 2008, 09:45 AM
Can anyone see what I'm doing wrong I'm getting an ArgumentNullException: Argument column can't be null ?



...

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

jshultz
February 1st, 2008, 09:53 AM
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.

TheCPUWizard
February 1st, 2008, 10:48 AM
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.

Visslan
February 4th, 2008, 07:52 AM
Here is the right solution! =)


...

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