Carl Harrison
Carl Harrison

Reputation: 155

Reusing a Datatable or am I going about this the wrong way...: DataTable already belongs to another DataSet

VB.NET: I've always used bindingsource's with dataset and using the fill command and then populating grids etc - no problem, but I have had to use a programatically coded dataset as manipulation needs to go on behind the scenes due to the nature of what I am working on for the company I am employed by.

So, my code looks something like this: In a form, I have declared at the top (because I need it to be accessed from other sections of the form

     Dim Facility_Table As DataTable

Then in my load sub I am calling a sub (I'll cut everything down so its as short as possible)

    Private Sub CreateNewDataTable()

    Try
        Facility_Table.Clear()
        Facility_Table.Columns.Add("Level1", GetType(String))
        Facility_Table.Columns.Add("Level2", GetType(String))
    End Sub

Then calling another sub (refreshlist) which fetches some data from SQL Server and shovels it into the fields in new rows.

    'Facility_Table.Reset()
    Facility_Table.Clear()
    Facility_Table.NewRow()
    Facility_Table.Rows.Add(.Rows(Z).Item("Level1").ToString,
                            .Rows(Z).Item("Level2").ToString)

Then I call a refreshsource sub: - now this bit I do normally, different with binding a grid to a datasource and its not my code, something I lifted on the internet via a google search to work with my Datatable.

    Dim Dset As New DataSet()

        ' Dset.Tables.Remove(Facility_Table)
        Dset = New DataSet()
        Dset.Tables.Add(Facility_Table)
            Dim bs As BindingSource
            bs = New BindingSource()

            bs.DataSource = Dset
            bs.DataMember = Dset.Tables(0).TableName
            DataGridView2.DataSource = Nothing
            DataGridView2.DataSource = bs
            DataGridView2.Refresh()

and it works fine the first time - then if I click on my treeview to go to a different "Level1" and call the refreshlist sub I get the exception:

"DataTable already belongs to another DataSet."

as you can see, from the commented line, I've tried removing the table before re-adding it, but no luck.

I thought that just clearing the datatable and then re-populating it and re-binding it would be all that is needed but I have a feeling that I have missed a step somewhere and Im hoping its just going to be a one liner or at worst just a couple of additional lines of code.

Any help would be most appreciated, thanks for your time!

Upvotes: 2

Views: 1169

Answers (2)

Carl Harrison
Carl Harrison

Reputation: 155

After trying various swaps and resets and god knows what, I ended up looking back at how I bind datatables to datagrids and it slapped in the face like a wet kipper.

Removing this lot:

            Dim Dset As New DataSet()
            'Dset = New DataSet()
            Dset.Tables.Add(Facility_Table)
            Dim bs As BindingSource
            bs = New BindingSource()

            bs.DataSource = Dset
            bs.DataMember = Dset.Tables(0).TableName
            DataGridView2.DataSource = Nothing
            DataGridView2.DataSource = bs
            DataGridView2.Refresh()

and replacing it with just:

    DataGridView2.DataSource = Facility_Table

Job is done. Im going to bed now, kipper in face hurts.

Upvotes: 0

Steve
Steve

Reputation: 216333

This error happens because the DataTable 'remembers' the dataset to which it belongs. This association is built when you add the table to the DataSet tables property. So, if you really want to keep the same DataTable around and you want to add it to another DataSet you need first to remove the table from the previous DataSet.

Dset = New DataSet()
Facility_Table.DataSet?.Tables.Remove(Facility_Table)
Dset.Tables.Add(Facility_Table)

Upvotes: 1

Related Questions