Removing association between DataSet and XmlDataDocument

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
 
J

JD

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.

<snip>

OK I figured something out myself. Shows how much it can help to
actually write out a description of your problem, I had been
scratching my head on this for 2-3 hours before I posted!

Now I call the sub with a /copy/ of the DataSet, and then do a
DataSet.Dispose just before the end of the sub, and then I don't get
any error message. Is this an efficient way to do it?
 

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