Error Message - Adding Table to Dataset

G

Guest

I'm trying to add a table to a dataset but get the error:

"A DataTable named 'BordDates0040' already belongs to this DataSet."

Code:
Dim dtBordDates As DataTable
dtBordDates = LoadTable(strDBPath & strDBName, strTableName,
strSQL)

Dim ds2 As DataSet = New DataSet()
Dim dt2 As DataTable = New DataTable()
dt2 = dtBordDates.Copy()
ds2.Tables.Add(dt2) '**** Error happens here ****

Any Suggestions?

Thanks, Mark
 
G

Guest

Cor -

This worked, thanks.

Its interesting, I had to change the TableName property of thefirst table:
dtBordDates

eg.
Dim dtBordDates As DataTable
dtBordDates = LoadTable(strDBPath & strDBName, strTableName,
strSQL)

dtBordDates.TableName = "BordDates" '***** New Code works *****

Dim ds2 As DataSet = New DataSet()
Dim dt2 As DataTable = New DataTable()
dt2 = dtBordDates.Copy()
ds2.Tables.Add(dt2)

I had tried unsuccessfully to change the TableName of the new DataTable:
dt2.TableName = "BordDates" '***** Did not work *****

Thanks, Again
Mark
 
G

Guest

Cor -

I've played around and was able to get it too work with the following code:

Dim dtBordDates As DataTable
dtBordDates = LoadTable(strDBPath & strDBName, strTableName, strSQL)

Dim ds2 As DataSet = New DataSet()

ds2.Tables.Add(dtBordDates.Copy()) '**** New Code ****

So, I'm able to add a table to the dataset directly without renaming it.
Less code is better.

Any issues come to mind? Seem OK to you?

Thanks, Mark
 
J

Jim Underwood

Mark,
Just trying to understand...

Is it your intent to have multiple copies of the same table in your dataset?

I'm not sure, but it sounds like you are adding the same table more than
once, but unintentionally. If the table exists and is not supposed to, then
it is probably because you are calling the code more than once.
 
C

Cor Ligthert [MVP]

Mark,

Sorry I did more look at what you wrote than to the problem (That is as soon
as I see methods as "loadDataTable" I stop reading because than I can not
see what is behind that. (I could have guessed that simple one, however I
did not).

You can even do
\\\
dim ds2 as New dataset
ds2.tables.add(dtBordDates.Copy)
///
Have a look as well to that assignment of the datatable, often samples are
translated C# samples and therefore the longer needed C# code is used.

Sorry that I did not see it direct.

I hope this helps anyhow

Cor
 
G

Guest

Jim -

My intention is to have several unique tables in the Dataset (I would think
this is the usual practice).

I really don't get why ADO.NET was returning the error message ("A DataTable
named 'BordDates0040' already belongs to this DataSet.") as the Dataset being
populated was empty.

I apparently had some syntax incorrect (who knows what).

The final code I arrived at was the very concise and direct:

Dim ds2 As New DataSet
ds2.Tables.Add(LoadTable(strDBPath & strDBName, strTableName, strSQL).Copy)

It excludes the "dt2" datatable object altogether. Sometimes (alot of
times) it takes alittle adversity to see the light.

Thanks Mark
 

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

Top