datatable already belongs to another dataset

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

Hi,
I have this error message with the following code :

m_dsSysInfo = New DataSet
Dim dt As DataTable = m_fields.GetAllTables()
m_dsSysInfo.Tables.Add(dt)

Why??? GetAllTables return a single datatable, so it should work ?!


Thx
 
Sam said:
Hi,
I have this error message with the following code :
Dim dt As DataTable = m_fields.GetAllTables()
m_dsSysInfo.Tables.Add(dt)

Why??? GetAllTables return a single datatable, so it should work ?!
Why should it work?

That would be when it returned a *new* datatable.

Cor
 
So if my function GetAllTables creates a new datatable and fill it with
information and then returns it, how can I add this returned table to
my dataset ?

Thx
 
What type of object is m_fields? And where does it get the datatable?
The exception is pretty clear. It seems you are trying to add a
DataTable to a DataSet but that DataTable already belongs to another
DataSet.
 
m_fields is of type of the class the method GetAllTables belongs to...
As I said GetAllTables return a single datatable (don't get confused by
the name of the function) created in this function. Then the returned
datatable is added to my dataset. I really don't see to which other
dataset it could belong to......
 
Public Function GetAllTables() As DataTable
Dim da As New DatabaseAccess

Try
m_dtFields = da.GetTable("select table_name from
INFORMATION_SCHEMA.TABLES", DatabaseAccess.SQLType.Text)
Return m_dtFields
Catch ex As Exception
Stop
End Try
End Function

databaseAccess is a class of mine that handle connections, commands....
The return m_dtFields datatable is correct and contains the right
values. It is a member variable of the class and was initialised using
New. It is not contained in any other dataset.

thx
 
Sam,

I am not so sure of that, however you can try that yourself when what you
say is true, than this should give the same error.

..m_dsSysInfo = New DataSet
Dim dt As New DataTable
m_dsSysInfo.Tables.Add(dt)

You can slowly go further in your program and let it return every time that
new datatable.

Cor
 
Cor,
I've solved my problem.
I had to return a copy of the datatable:

Return m_dtFields.copy

and that works fine.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top