Jump to content

.txt file becomes blank even if not overwritten

AliB2512

I have code that fills textboxes with each line of a text file, and as such allows you to edit them and update the .txt file.

only if the password is entered correctly twice will it update, if not it produces a message box telling you what's happened (i.e passwords wrong or left blank) however when the if statement falls down the first stream of (password field cannot be blank) it doesn't update the file (good) but complete removes what's saved, giving a blank .txt file

 

Private Sub btnUpdateUser_Click(sender As Object, e As EventArgs) Handles btnUpdateUser.Click
    Dim allLines As List(Of String) = New List(Of String)
    Dim path As String = "..\Resources\Workers\" + txtEmployeeName.Text + ".txt"
    If System.IO.File.Exists(path) = False Then
        MsgBox("File Does Not Exists")
    Else

        If (txtPassword.Text = String.Empty And txtPassword2.Text = String.Empty) Then
            MsgBox("Password Fields cannot be empty")
        ElseIf (txtPassword.Text = txtPassword2.Text) Then
            Dim reader As New System.IO.StreamReader(path)
            Do While Not reader.EndOfStream
                allLines.Add(reader.ReadLine())
            Loop
            reader.Close()
            Dim varSalt As String = CreateRandomSalt()
            System.IO.File.Delete(path)
            File.Create(path).Dispose()
            Dim objWriter As New System.IO.StreamWriter(path)
            objWriter.WriteLine(Me.txtEmployeeName.Text)
            objWriter.WriteLine((Hash512(Me.txtPassword.Text, varSalt) & vbNewLine & varSalt))
            objWriter.WriteLine(Me.txtFirstname.Text)
            objWriter.WriteLine(Me.txtSurname.Text)
            objWriter.WriteLine(Me.dtpBirthday.Text)
            If (Me.cbxManager.Checked) Then
                objWriter.WriteLine("true")
            Else
                objWriter.WriteLine("false")
            End If
            objWriter.Close()
            MsgBox("User Updated")
            Me.Close()
            settingsForm.Show()
        Else
            MsgBox("Passwords do not match")
        End If
    End If
End Sub

the text file starts as

 Username
 Password
 salt
 First name - Error is Here 
 Surname
 D.O.B
 Manager Status 

if password is correct the txt file is

Username
 Password
 salt
 First name - Error is Fixed
 Surname
 D.O.B
 Manager Status 

However when the if statement states that "the password field cannot be blank" the .txt file become blank

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×