J
JD
I have a DataGridView with a DataSet as DataSource. The user can
update the contents of the DataGridView, and then click on a Save
button to save the data to an XML file.
When they click on Save, I call the subroutine below. The first time
that I call it, it works fine. However, if the user makes some more
changes in the DataGridView and then wants to save again, I get the
following error message:
"DataSet can be associated with at most one XmlDataDocument. Cannot
associate the DataSet with the current XmlDataDocument because the
DataSet is already associated with another XmlDataDocument."
This occurs on the line that I indicated. I can see what's wrong, but
I can't work out how to remove the association between the DataSet and
the XmlDataDocument at the end of the sub. Tried adding the line
"xmlDoc = Nothing" at the end, but didn't work.
Private Sub SaveDataSetToXml(ByVal filePath, ByRef DataSet)
Dim xmlDoc As System.Xml.XmlDataDocument
Dim xmlDec As System.Xml.XmlDeclaration
Dim xmlWriter As System.Xml.XmlWriter
xmlWriter = New
System.Xml.XmlTextWriter(My.Computer.FileSystem.OpenTextFileWriter(filePath,
False))
xmlDoc = New System.Xml.XmlDataDocument(DataSet) ' This is the line
that gives an error
xmlDoc.DataSet.EnforceConstraints = False
xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
xmlDoc.PrependChild(xmlDec)
xmlDoc.WriteTo(xmlWriter)
xmlWriter.Flush()
xmlWriter.Close()
End Sub
update the contents of the DataGridView, and then click on a Save
button to save the data to an XML file.
When they click on Save, I call the subroutine below. The first time
that I call it, it works fine. However, if the user makes some more
changes in the DataGridView and then wants to save again, I get the
following error message:
"DataSet can be associated with at most one XmlDataDocument. Cannot
associate the DataSet with the current XmlDataDocument because the
DataSet is already associated with another XmlDataDocument."
This occurs on the line that I indicated. I can see what's wrong, but
I can't work out how to remove the association between the DataSet and
the XmlDataDocument at the end of the sub. Tried adding the line
"xmlDoc = Nothing" at the end, but didn't work.
Private Sub SaveDataSetToXml(ByVal filePath, ByRef DataSet)
Dim xmlDoc As System.Xml.XmlDataDocument
Dim xmlDec As System.Xml.XmlDeclaration
Dim xmlWriter As System.Xml.XmlWriter
xmlWriter = New
System.Xml.XmlTextWriter(My.Computer.FileSystem.OpenTextFileWriter(filePath,
False))
xmlDoc = New System.Xml.XmlDataDocument(DataSet) ' This is the line
that gives an error
xmlDoc.DataSet.EnforceConstraints = False
xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
xmlDoc.PrependChild(xmlDec)
xmlDoc.WriteTo(xmlWriter)
xmlWriter.Flush()
xmlWriter.Close()
End Sub