none
DataTable already belongs to another DataSet error. RRS feed

  • Question

  • I have a DataSet named as dsResult which has the resultset from a stored procedure.

    This DataSet has totally 3 tables in it. What I am trying to do is create a new dataset and assign only one table from this dsResult to the new dataset.

    DataSet ds = new DataSet();

    DataTable dt = new DataTable();

    dt = dsResult.Tables[0];

    ds.Tables.Add(dt);

    Than I am assigning this ds dataset to a Report Chart object function as follows,

    ReportChart1.DataSource = ds;

    But I am getting Server error when running the dotnet page as follows,

    System.ArgumentException: DataTable already belongs to another DataSet.

    ds.Tables.Add(dt); <-- This line in red color

     

    Please tell how to correct this. Thanks.

     

    Wednesday, December 1, 2010 11:37 AM

Answers

  • You cannot move or assign DataTable from one DataSet into another one. It is an object reference and in this case multiple DataSets would contain reference to same DataTable instance. You can create copy of the DataTable using Copy method of it and then assign that DataTable to new DataSet

     


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by liurong luo Wednesday, December 8, 2010 11:04 AM
    Wednesday, December 1, 2010 11:51 AM
    Moderator

All replies

  • You cannot move or assign DataTable from one DataSet into another one. It is an object reference and in this case multiple DataSets would contain reference to same DataTable instance. You can create copy of the DataTable using Copy method of it and then assign that DataTable to new DataSet

     


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by liurong luo Wednesday, December 8, 2010 11:04 AM
    Wednesday, December 1, 2010 11:51 AM
    Moderator
  • ds.Tables.Add(dt.Copy());

    will work...

    Friday, February 17, 2012 2:03 AM