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?

Edited 2 Years Ago by VUEKID

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.

Edited 2 Years Ago by cgeier

Hi Vuekid,

I would like to receive your code for converting to xml and back.

thx in advance, Bunnie

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

This article has been dead for over six months. Start a new discussion instead.