If you don't need anymore the previous DataSet but want to use the same global variable then just reinitialize it
ssrsReportDataSet = new DataSet();
And, no there is no need to Dispose a DataSet.
Question
How do I clear a dataset when the tables have existing relations? I'm trying to clear a dataset between uses. Each time I load a new XML file the dataset keeps the old table names, but I'm loading an entirely new XML file so I need it cleared out. I'm trying to use ssrsReportDataSet.Tables.Clear()
, but I get
"Cannot remove a table that has existing relations. Remove relations first."
I'm pretty sure there are foreign key constraints between the existing tables.
Previously I have tried walking the dataset and deleting each row from each table, but I still can't delete the tables or clear the dataset.
Private Sub btnLoadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadFile.Click
Dim fd As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String
fd.Title = "Open File Dialog"
fd.InitialDirectory = "C:\"
fd.Filter = "XML Files (*.xml)|*.xml|Excel Files (*.xls)|*.xlsx"
fd.FilterIndex = 1
fd.RestoreDirectory = True
If fd.ShowDialog() = DialogResult.OK Then
' Clear data set
ssrsReportDataSet.Tables.Clear()
strFileName = fd.FileName
lblFilePath.Text = strFileName
'Load dataset from XML file
ssrsReportDataSet.ReadXml(strFileName)
' Set dropdown values
For n As Integer = 0 To ssrsReportDataSet.Tables.Count - 1
tableSelecter.Items.Add(ssrsReportDataSet.Tables(n).TableName)
Next
' Load datagrid
DataGridViewXml.DataSource = ssrsReportDataSet
DataGridViewXml.DataMember = ssrsReportDataSet.Tables(0).TableName
tableSelecter.SelectedIndex = 0
End If
End Sub
Solution
If you don't need anymore the previous DataSet but want to use the same global variable then just reinitialize it
ssrsReportDataSet = new DataSet();
And, no there is no need to Dispose a DataSet.