none
Adding DataTable to DataSet gives "datatable already belongs to another dataset" RRS feed

  • Question

  • Hi all,

    I've got some VB 2010 code where I am assigning a datatable to a dataset; however, I am getting the Datatable already belongs to another dataset message. Here is my test code that demonstrates this issue:

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim dtEx As DataTable Dim dteDate As Date = DateTime.Now dtEx = New DataTable("MyData") dtEx.Columns.Add("ACount", GetType(Integer)) dtEx.Columns.Add("ADate", GetType(DateTime)) dtEx.Rows.Add(New Object() { 201, dteDate }) ProcTable(dtEx) dtEx.Clear() dtEx.Rows.Add(New Object() { 56, dteDate }) ProcTable(dtEx) End Sub Private Sub ProcTable(ByVal dt As DataTable) Using ds As New System.Data.DataSet() ds.Tables.Add(dt) End Using End Sub

    I solved this issue by changing ds.Tables.Add(dt) to ds.Tables.Add(dt.Copy())

    Is this the best way to solve this issue? I could re-instantiate the dt object, but then I would also have to add the column definitions. I would not have thought that it would have a problem since ds goes away after the Using block ends and the datatable is passed by value. I would like to understand the reason behind this error. Thanks! Saga


    Insanity is the prelude to discovery

    Friday, November 7, 2014 8:14 PM

Answers

  • Yes, the dt.Copy() is the correct way to get around this. The issue arises because there is a read-only property, DataSet, in the DataTable which gets set when the DataTable is added to a DataSet. Even though the DataSet gets disposed after the Using ends, the property is still set in the DataTable. So now the DataSet property in the DataTable contains an invalid DataSet name, but there's no way to re-set the DataSet property back to null.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Proposed as answer by Fred BaoModerator Monday, November 10, 2014 2:36 AM
    • Marked as answer by SagaV9 Monday, November 10, 2014 5:02 PM
    Monday, November 10, 2014 12:24 AM

All replies

  • Yes, the dt.Copy() is the correct way to get around this. The issue arises because there is a read-only property, DataSet, in the DataTable which gets set when the DataTable is added to a DataSet. Even though the DataSet gets disposed after the Using ends, the property is still set in the DataTable. So now the DataSet property in the DataTable contains an invalid DataSet name, but there's no way to re-set the DataSet property back to null.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Proposed as answer by Fred BaoModerator Monday, November 10, 2014 2:36 AM
    • Marked as answer by SagaV9 Monday, November 10, 2014 5:02 PM
    Monday, November 10, 2014 12:24 AM
  • Thanks for your input. And of course passing in the dt object by value does not help at all:-). Regards, Saga 

    Insanity is the prelude to discovery


    • Edited by SagaV9 Monday, November 10, 2014 5:21 PM typo
    Monday, November 10, 2014 5:02 PM