Start New Discussion within our Software Development Community

I am trying to write the information below (name, employee no., etc.) to a file using StreamWriter. I'm able to enter the data, click the save button, enter/change the save location and file name and save the document. When I open the saved document, its blank and none of the data is written. Any advice?

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim Save As New SaveFileDialog()
        Dim EmployeeDataFile As StreamWriter

        'Configure Save Record
        Save.Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*"
        Save.InitialDirectory = "C:\"
        Save.ShowDialog()

        'Save employee data
        Try
            EmployeeDataFile = System.IO.File.AppendText(Save.FileName)
            EmployeeDataFile.WriteLine(txtFirstName.Text)
            EmployeeDataFile.WriteLine(txtMiddleName.Text)
            EmployeeDataFile.WriteLine(txtLastName.Text)
            EmployeeDataFile.WriteLine(txtEmpNumber.Text)
            EmployeeDataFile.WriteLine(cboDepartment.Text)
            EmployeeDataFile.WriteLine(txtTelephone.Text)
            EmployeeDataFile.WriteLine(txtExtension.Text)
            EmployeeDataFile.WriteLine(txtEmail.Text)
        Catch ex As Exception
            MessageBox.Show("Error creating the file.")
        End Try
    End Sub

A little more research and I came across the flush method. I don't fully understand why, but it solved the problem.

This question has already been answered. Start a new discussion instead.