Using: Visual Studio
Language: Visual Basic

I already loaded a xml file to my datagrid already, what i am trying to do is have the option to save the datagrid data back as an xml file. I have the code, but how do I close the original xml file that is in use in the datagrid so that it will let me save the file back to the file?

        'define a save dialog
        Dim save_file As New SaveFileDialog
        'give its extension
        save_file.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*"
        'select xml
        save_file.FilterIndex = 2
        'create a datatable
        Dim my_datatable As New DataTable
        'if ok click
        If save_file.ShowDialog() = DialogResult.OK Then
            'get datagridview source to datatable
            my_datatable = CType(DataGridView1.DataSource, DataTable)
            'save datatable with xml format
            my_datatable.WriteXml(save_file.FileName)
        End If

When i try to save it to the same file i loaded it to the datagrid, error says file already in use, cannot save. How do i bypass that or update the grid and at the same time update the file?

Recommended Answers

All 3 Replies

You haven't posted enough of your code. "File in use" errors are a result of a file being open, and trying to modify it when it has a lock on it. Therefore, you need to post the code that opens and uses the file.

After saving file you must have close & dispose the Datable variable. And also close the opened file stream, inwhich you save the XmlFile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.