Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenFileDialog string problems

Status
Not open for further replies.

dbrb2

Instructor
Jul 19, 2004
121
GB
Hello,

Sorry to post twice in two days...
I have a datagridView component, that uses as its source an XML file.

Code:
  Private Sub loadFromXML()
        Try
            ds.ReadXml(filePath, XmlReadMode.InferSchema)
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox("Error: Can't load config.xml")
        End Try

    End Sub

As the datagridview is changed, I save it back to the XML.


To add a row, I have a textbox I can type into, then a button that calls:

Code:
Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
        Dim curRow = DataGridView1.CurrentCell.RowIndex.ToString()
        Dim curTime = DataGridView1.Rows(curRow).Cells(1).Value
        If (pathTextBox.TextLength <> 0) Then
            If (curTime = "") Then
                DataGridView1.Rows.RemoveAt(DataGridView1.CurrentCell.RowIndex.ToString())
            End If
            Dim newRow As System.Data.DataRow = ds.Tables(0).NewRow

            Dim path As String = pathTextBox.Text
            Dim array = path.ToArray
            newRow.ItemArray = New Object() {path, numberChooser.Value, 0}

            ds.Tables(0).Rows.Add(newRow)
            DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(0).Selected = True
            saveToXML()
        End If
    End Sub

This adds to the datagridview, and also updates the XML file:

Code:
    Private Sub saveToXML()
        ds.WriteXml(filePath, XmlWriteMode.IgnoreSchema)
    End Sub

This all works fine. However, I have the option to fill the textbox above using an OpenFileDialog box. If I do this, rather than typing a string into the textbox manually, then although the Datagridview is updated, the XML file is not.

What is more, once I have once tried to use the Openfiledialog, even entering tect manually will not cause the XML to be updated uitil I restart the program.

I have compared the hand typed / generated by Openfiledialog strings, and they appear identcal. I found a thread on this forum which sounded similar, but does not appear to help me here.

Does anyone have any ideas?

Cheers
 
I have investigated further, and if at the save to XML stage I do this:

Code:
MsgBox(ds.GetXml())
ds.WriteXml(filePath) , XmlWriteMode.IgnoreSchema)

Then the message box displays the correct XML, event though the data is not written to file.

 
Ok, this is getting stranger and stranger...

I wondeed whether the config file was locked in some way. So I changed my writeToXML function to the following:

Code:
 Private Sub saveToXML()
        MsgBox(ds.GetXml())

        Dim FILE_NAME As String = "test2.txt"

        Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
        objWriter.Write("foo")
        objWriter.Close()

        ds.AcceptChanges()
        ds.WriteXml(filePath)
    End Sub

As before, if I have not, populated a textbox using my file open dialog, everything is fine. My XML and "foo" files are both created.

However, if I have populated the textbox using the dialog, then not only does my XML not get updated, but the "foo" file doesn'y get created either, and surely it is entirely standalone?

It looks like my using the dialog has somehow stopped me from being able to write to any files at all, but I can't think why this should be...
 
Its fixed :)

I added:

Code:
 Dim cwd = System.IO.Directory.GetCurrentDirectory()
    Dim configFile As String = "config.xml"
    Dim filePath As String = cwd + "\" + configFile

To my code, rather than just relying on the fact that the config file was in the same directory as the executable.

At a guess, when I first loaded the program, the working directory is the same as the exe file, and so I cna load/append to the file OK. But it looks like the OpenFileDialog box changes the working directory, hence my problems. By using the absolute path, this problem is avoided.

How annoying!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top