locked
How to make file new, file open and Save file with Datatable WriteXML and ReadXML RRS feed

  • Question

  • Hallo , 

    I have made this example, i can save file as xml and read back to dataset using readxml but i dont know how to create new file or empty dataset with button click for creating new file from empty dataset. How it should be done?

    Public Class Form1
    
        Public DS As DataSet = New DataSet("Project")
        Public table1 As DataTable = New DataTable("Medications")
        Public BS As New BindingSource
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            With table1
                .Columns.Add("id")
                .Columns.Add("medication")
            End With
    
            ' Create the DataSet.
            DS.Tables.Add(table1)
    
            BS.DataSource = DS.Tables("Medications")
            DataGridView1.DataSource = BS
    
    
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles save.Click
            DS.WriteXml("DS.xml")
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles new_row.Click
    
            table1.Rows.Add(1, "atenolol")
            table1.Rows.Add(6, "trifluoperazine")
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles open.Click
            DS.ReadXml("DS.xml")
        End Sub
    
    End Class

    Thanks 

    Friday, August 14, 2020 6:51 PM

Answers

  • Hi

    Not exactly sure what you want, but, as far as I understand, if you just clear the existing table and save it with a new file name you might have what you want?


    Regards Les, Livingston, Scotland

    • Marked as answer by Shan1986 Saturday, August 15, 2020 1:11 PM
    Friday, August 14, 2020 7:44 PM

All replies

  • Hi

    Not exactly sure what you want, but, as far as I understand, if you just clear the existing table and save it with a new file name you might have what you want?


    Regards Les, Livingston, Scotland

    • Marked as answer by Shan1986 Saturday, August 15, 2020 1:11 PM
    Friday, August 14, 2020 7:44 PM
  • Hi

    Not exactly sure what you want, but, as far as I understand, if you just clear the existing table and save it with a new file name you might have what you want?


    Regards Les, Livingston, Scotland

    So i just clear the dataset? This is how normally works or proper way to do it?. thanks

    Friday, August 14, 2020 8:33 PM
  • Hi

    To be able to claim 'proper way to do it' is not an easy thing in that there are usually many different ways to deal with any question.

    Look at it this way: if it works and achieves the objective - then it is the right way.  That doesn't mean that there may be other 'ways' which may be faster/less complex/more elegant solutions.

    I the case of your question: a DataTable is just that, and saved under a different name means that is would have the same layout but just no data. You could have several saved data files, all based on the same datatable but each containing different (or no) data. Of course, that type of idea means that the DataSet is redundant, because it would just be a bunch of separate datatables instead.


    Regards Les, Livingston, Scotland

    Friday, August 14, 2020 8:43 PM