Dataset Naming Question

  • Thread starter Thread starter jcrouse
  • Start date Start date
J

jcrouse

I have the following code that creates a couple of XML files:

Dim dt1 As New DataTable("P1JoyUp")

If H = True Then

Dim dsH As New DataSet("HCPViewer")

dsH.Tables.Add(dt1)

dt1.Columns.Add("Top")

Dim dr1 As DataRow = dsH.Tables("P1JoyUp").NewRow

dr1("Top") = lblP1JoyUp.Top.ToString

dsH.Tables("P1JoyUp").Rows.Add(dr1)

dsH.WriteXml(SaveFileDialog1.FileName)

End If



If V = True Then

Dim dsV As New DataSet("VCPViewer")

dsV.Tables.Add(dt1)

dt1.Columns.Add("Top")

Dim dr1 As DataRow = dsV.Tables("P1JoyUp").NewRow

dr1("Top") = lblP1JoyUp.Top.ToString

dsV.Tables("P1JoyUp").Rows.Add(dr1)

dsV.WriteXml(SaveFileDialog1.FileName)

End If



If R = True Then

Dim dsrH As New DataSet("HCPViewer")

dsrH.Tables.Add(dt1)

dt1.Columns.Add("Top")

Dim dr1 As DataRow = dsrH.Tables("P1JoyUp").NewRow

dr1("Top") = lblP1JoyUp.Top.ToString

dsrH.Tables("P1JoyUp").Rows.Add(dr1)

dsrH.WriteXml(SaveFileDialog1.FileName)



Dim dsrV As New DataSet("VCPViewer")

dsrV.Tables.Add(dt1)

dt1.Columns.Add("Top")

Dim dr1 As DataRow = dsrV.Tables("P1JoyUp").NewRow

dr1("Top") = lblP1JoyUp.Top.ToString

dsrV.Tables("P1JoyUp").Rows.Add(dr1)

dsrV.WriteXml(SaveFileDialog1.FileName)

End If


If condition "H" is true I create a dataset HCPViewer and add a table dt1.
This works great.
If condition "V" is true i create a dataset VCPViewer and add a table dt1.
This also works great
If condition "R" is true I can run throught the "H" logic but when I get to
the "V" logic it errors out on the "dsrV.Tables.Add(dt1) line with the
following error:

An unhandled exception of type 'System.ArgumentException' occurred in
system.data.dll

Additional information: DataTable already belongs to another DataSet.

Is it true that I can't have the same table name in two different datasets
They are being written to completely different files. If I can't do this,
and it seems I can't, what are my options here?

Thank you,
John
 
A datatable can belong to only one dataset.

Your can remove the datatable from the first dataset, add it to the second.
Do your processing, then remove it again and place it back in the original
dataset.
 
Hi John,

I think you are looking for dataset.clone or/and dataset.copy here.

Have a look on MSDN for that.

Cor
 
Back
Top