0

UPDATE Thanks vlam, I am now writing to Stream as you suggested, I get an XML file and no more errors, my XML file only has this, no data,

<?xml version="1.0"?>
<RoboDataSet/>

Why do I not have data?

I have a datagridview and a dataset on a form. The form opens, I type data in the first 2 rows, then I click a write XML button I added to the form. Code for the button and the WriteXML is here. It creates a file, which is empty. But when it tries to execute the RoboDataSet.WriteXml(filename) command, I get an error.

After I type data in the DataGridView is it saved in the DataGridView and the DataSet at that point?

I have a message box, displays we have data, so I believe the data I entered is in the dataset. Then I step the code and I see a file created in c:\data, then on the RoboDataSet.WriteXml(filename) command I get this error;

So a few questions, why do I get the error being used, it is the same process trying to writexml, correct?

I tried 2 different ways, based on MS examples I saw,

Example 1
Dim stream As New System.IO.FileStream _
    (filename, System.IO.FileMode.Create)
 thisDataSet.WriteXml(stream)

Example 2
Dim filename As String = "XmlDoc.xml"
thisDataSet.WriteXml(filename)

and here is my code that is not working;

Private Sub WriteXml_Click(sender As Object, e As EventArgs) Handles WriteXml.Click
    WriteXmlToFile(RoboDataSet)
End Sub

Private Sub WriteXmlToFile(RoboDataSet As DataSet)
    If RoboDataSet Is Nothing Then
        MessageBox.Show("dataset empty")
    Else
        MessageBox.Show("We have data")
    End If
    Dim filename As String = "c:\data\write4.xml"
    Dim Stream As New System.IO.FileStream _
        (filename, System.IO.FileMode.Create)
    RoboDataSet.WriteXml(filename)




End Sub

Any help would be great, thanks

Thanks vlam, I am now writing to Stream as you suggested, I get an XML file and no more errors, my XML file only has this, no data,

Why do I not have data?

yellow
  • 13
  • 6
  • You have mixed `.WriteXML(Stream)` overload with `.WriteXML(String) Either will work but when you created a stream, the stream is busy using the created file. So, when you tried to use the `.WriteXML(String)` it will fail. – Mary Feb 27 '19 at 06:43

2 Answers2

1

Should be writing to a stream and not the filename. Also only write if dataset is not nothing.

Private Sub WriteXmlToFile(RoboDataSet As DataSet)
    If RoboDataSet Is Nothing Then
        MessageBox.Show("dataset empty")
    Else
        MessageBox.Show("We have data")
        Dim filename As String = "c:\data\write4.xml"
        Dim Stream As New System.IO.FileStream(filename, System.IO.FileMode.Create)
        RoboDataSet.WriteXml(Stream)
    End If
End Sub
Vlam
  • 1,622
  • 1
  • 8
  • 17
0

The FileStream class implements IDisposable so it needs to be closed and disposed. (Avoid leaks of unmanaged resources, file handles and such)

The Using block will handle this even if there is an error.

This answer is the same as @Vlam 's answer with the addition of the Using block. @Vlam is the one who diagnosed the error so please accept that answer but do add the Using block.

Private Sub WriteXmlToFile(RoboDataSet As DataSet)
    If RoboDataSet Is Nothing Then
        MessageBox.Show("The DataSet does not exist.")
        Return
    End If
    '**EDIT**
    For Each t As DataTable In RoboDataSet.Tables
        Debug.Print(t.Rows.Count.ToString)
    Next
    '**END EDIT**

    Dim filename As String = "c:\data\write4.xml"
    Using Stream As New System.IO.FileStream(filename, System.IO.FileMode.Create)
        RoboDataSet.WriteXml(Stream)
    End Using
End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27
  • I have used Vlam's answer, but not I get a XML file with just this in it; – yellow Feb 27 '19 at 07:15
  • Thanks, I am using Vlam's suggestion and will add yours, but now I get a xml file with just this in it; – yellow Feb 27 '19 at 07:16
  • I suspect you have no data in your DataSet. See my edit to check. – Mary Feb 27 '19 at 07:30
  • Your edit did not show anything but i entered data I tried with the datagridview bound and unbound and no change clearly I am missing something I am new – yellow Feb 27 '19 at 07:43
  • I think you need a new question showing the code you use to create the DataSet and how it is bound to the DataGridView. Explain how data is entered and ask why it is not showing up in the DataSet. – Mary Feb 27 '19 at 07:48