checking for file error.

drew4663

Well-known member
Joined
May 3, 2007
Messages
62
Programming Experience
1-3
I have a text file that contains three lines.
1. Username
2. Password
3. Time Stamp

When creating a password it takes the username and makes the username the name of the file. Example: username: "Andrew" file: "Andrew.txt".

When I try to read the file it works great. When I put in the wrong password of the right file name it gives a custom message box. When I put in a username that has not been created or a blank username it gives a custom error message but continues to error out saying there is no such file found. here is the code.

VB.NET:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If UsernameTextBox.Text = "" And PasswordTextBox.Text = "" Then
            MsgBox("You must enter a username and password.")
        End If
        Dim user As String
        user = UsernameTextBox.Text

        Using sr As StreamReader = New StreamReader(user + ".txt")
            If sr.ReadLine.Contains("" + ".txt") And sr.ReadLine.Contains("" + ".txt") Then
                MsgBox("You must enter a username and password.")
            ElseIf sr.ReadLine.Contains(UsernameTextBox.Text) And sr.ReadLine.Contains(PasswordTextBox.Text) Then
                MsgBox("Yes")
            Else
                MsgBox("You have entered an incorrect username/password.")
            End If
        End Using

    End Sub

Any help would be appreciated. i feel it is something that is simple but I have been looking at it for so long I can't seem to pin point it. Thank you.
 
Ok, I got part of it to work. When the person leaves it blank I just added this code....

If user = "" Then
MsgBox("Please enter a username/pssword")
Return
End If

But I'm still not sure what to do if the enter a name that isn't on the list.
 
I finally got it. Code posted below.

VB.NET:
        Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim user As String
        user = UsernameTextBox.Text
        If user = "" Then
            MsgBox("Please enter a username/pssword")
            Return
        End If
        Try
            Using sr As StreamReader = New StreamReader(user + ".txt")
                If sr.ReadLine.Contains(UsernameTextBox.Text) And sr.ReadLine.Contains(PasswordTextBox.Text) Then
                    MsgBox("Yes")
                Else
                    MsgBox("You have entered an incorrect username/password.")
                End If
            End Using
        Catch X As FileNotFoundException
            MsgBox(UsernameTextBox.Text + "is not in our records.  Please try again.")
            Return
        End Try

    End Sub
 
Back
Top