CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2006
    Posts
    170

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

  2. #2
    Join Date
    Nov 2007
    Posts
    110

    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.

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    May 2006
    Posts
    170

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured