none
copy datatable to another datatable in dataset? RRS feed

  • Question

  • Hello,

     

    I am trying to copy a populated datatable to another datatable in a tables set.

     

    Why does this not work?

     

    ds.Tables[0] = dt.copy();

     

    The error is that "this can not be assigned to, it is read-only"

     

    Thanks,

    Kim

    Tuesday, August 7, 2007 3:32 PM

Answers

  • If your table structures are identical then you could try the "Merge" method on the DataTable... check it out and see if that works.

    Tuesday, August 7, 2007 3:43 PM
  • Below is a snippet from the help file.  I looked at the "DataSet.Tables" property to see that "Tables" is a "DataTableCollection"... so I looked that up in the help file and went to it's "Item" property (which is synonymous with the indexer as you used it "Tables[0]").  As you can see, "Item" (or the indexer) only has a "get", which means it is read-only.  Whereas, declaring a new DataTable then setting it equal to some other DataTable does not carry that restriction, as it is a restriction of the "DataTableCollection", not the DataTable.  I worded that poorly, but does that make sense?

     

    Code Snippet
    public DataTable this [ int index] { get; }

     

     

    Tuesday, August 7, 2007 5:17 PM

All replies

  • If your table structures are identical then you could try the "Merge" method on the DataTable... check it out and see if that works.

    Tuesday, August 7, 2007 3:43 PM
  • Thanks!  That did work.  But I am a little stumped on why the copy() and clone() does not work when references like:

     

    ds.Tables[0] = dt.clone();

     

    But if you declare a DatatTable newDt and newDt.copy will work.  Any insight would be appreciated.

     

    Thanks again,

    Kim

    Tuesday, August 7, 2007 4:42 PM
  • Below is a snippet from the help file.  I looked at the "DataSet.Tables" property to see that "Tables" is a "DataTableCollection"... so I looked that up in the help file and went to it's "Item" property (which is synonymous with the indexer as you used it "Tables[0]").  As you can see, "Item" (or the indexer) only has a "get", which means it is read-only.  Whereas, declaring a new DataTable then setting it equal to some other DataTable does not carry that restriction, as it is a restriction of the "DataTableCollection", not the DataTable.  I worded that poorly, but does that make sense?

     

    Code Snippet
    public DataTable this [ int index] { get; }

     

     

    Tuesday, August 7, 2007 5:17 PM
  • It does.  Thanks for the information and the lookup.

     

    Thanks again,

    Kim

     

    Tuesday, August 7, 2007 7:55 PM