10 Replies - 9197 Views - Last Post: 08 October 2012 - 05:01 AM Rate Topic: -----

#1 lodman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-July 11

Login using text files as a database

Posted 15 July 2011 - 06:45 PM

Hi, im having trouble with my programme which is a login and register system using text files to store the details of the users.

How it works is the user registers their details on one form which will append those details to the end of the 2 text files that are holding the data.
At the moment this hasn't been implemented so we're just doing this manually.
Anyway, after that the user can "login" inputting a user name and password into 2 text boxes and the program looks for those details in the text files that they just registered.
I want the programme to read this by using "readline" in 2 text files and comparing what they've written in the text box with the corresponding text file and then checking whether those 2 words are on corresponding lines in the different text files, and so ensuring the user has put in the right username and password.

As I have average knowledge of vb.net and reading and writing to text files is new to me, I haven't really been able to debug it properly. But I had one idea to get the program to add an integer value at the beginning of each line and then get the program just to read the first character of each line and check if that was the same. But then I realised It ended up not having anything to do with the text boxes and I didn't know how to compare line by line anyway so It was pretty much pointless, but I'll include it in the code anyway.

On the form side there's just 2 text boxes "txtUsername" and "txtPassword" and a button for logging in "btnLogin"

  Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        Dim Usernamereader As String = "G:\Text1.txt"
        Dim PasswordReader As String = "G:\Text2.txt"
        Dim TextLineUser As String
        Dim TextLinePass As String
        Dim CountUser As Integer
        Dim CountPass As Integer
        Dim CharUser As Char
        Dim CharPass As Char

        If System.IO.File.Exists(Usernamereader) And System.IO.File.Exists(PasswordReader) = True Then

            Dim objReaderUser As New System.IO.StreamReader(Usernamereader)
            Dim objReaderPass As New System.IO.StreamReader(PasswordReader)

            Do While objReaderUser.Peek() <> -1
                CountUser = 0
                For CountUser = 1 To 10
                    CountUser = CountUser + 1
                    TextLineUser = TextLineUser & CountUser - 1 & _
                        objReaderUser.ReadLine() & vbNewLine
                Next
                CharUser = GetChar(TextLineUser, 1)
            Loop

            Do While objReaderPass.Peek() <> -1
                CountPass = 0
                For CountPass = 1 To 10
                    CountPass = CountPass + 1
                    TextLinePass = TextLinePass & CountPass - 1 & _
                        objReaderPass.ReadLine() & vbNewLine
                Next
                CharPass = GetChar(TextLinePass, 1)
            Loop

            If txtUsername.Text = TextLineUser And txtPassword.Text = TextLinePass Then
                MsgBox("you logged in!")
                MsgBox(TextLineUser)
                MsgBox(TextLinePass)
                MsgBox(CountPass)
                MsgBox(CountUser)

            Else
                MsgBox("not good")
                MsgBox(TextLineUser)
                MsgBox(TextLinePass)
                MsgBox(CountPass)
                MsgBox(CountUser)

            End If

        Else

            MsgBox("file does not exist")

        End If  


Is This A Good Question/Topic? 0
  • +

Replies To: Login using text files as a database

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: Login using text files as a database

Posted 15 July 2011 - 08:49 PM

Show an example of the text file. And how you save to the text files. You could save both the username and password to the same file and the same line and separate them with a char you can split it with when you read it. The other thing I see is you are not handling the closing of these streams - a must. So call the Close and Dispose methods or use a Using block on the StreamReaders. Instead of all those msgbox's Debug.Writeline would be must simpler.
Was This Post Helpful? 1
  • +
  • -

#3 lodman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-July 11

Re: Login using text files as a database

Posted 15 July 2011 - 09:21 PM

View Posthawkvalley1, on 15 July 2011 - 08:49 PM, said:

Show an example of the text file. And how you save to the text files. You could save both the username and password to the same file and the same line and separate them with a char you can split it with when you read it. The other thing I see is you are not handling the closing of these streams - a must. So call the Close and Dispose methods or use a Using block on the StreamReaders. Instead of all those msgbox's Debug.Writeline would be must simpler.



At the moment the text files are just;
User1
User2
User3
User4
User5

and

Pass1
Pass2
Pass3
Pass4
Pass5

so I would want User1 and Pass1 to correspond etc. line by line.

I haven't put in any save function yet, I was hoping to just use a similar concept of just appending text from a text box to the end of the line so it could be read later.

Could you give me some code for that? I'm still learning and yeah its obviously the kind of thing I was looking for that might fix this.

Would it be more efficient/easier to split it? I dunno how to code that either, i've read about it though. It just uses a # or whatever to differentiate the strings and moves on.

Thanks so much
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: Login using text files as a database

Posted 16 July 2011 - 06:20 PM

Some thing to talk about first.

Is this homework - it doesn't seem like it? Just cause they always limit what objects to use.

This is not be the best route for saving US/PW. Do you plan on any encryption - that would at least keep it safer?

As for delimiting it in one file is, use whatever you like

john|jhg123
bob|bgl234

Then as you read each line you make a string array using the string.split method.

'write the data
Using sw As New StreamWriter("path", True)
  used to append to file
End Using

Was This Post Helpful? 1
  • +
  • -

#5 lodman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-July 11

Re: Login using text files as a database

Posted 16 July 2011 - 08:55 PM

It's kind of like homework. But more just a personal learning thing for a bigger project that i know is coming. So yeah i really just want to learn as much as I can now and get a program going that I can use later.

ok, so that code appends to the file. Which I would use so the users could register their details.

But when the user tries to log in it still doesn't find the details in the text files.

Like it doesn't validate the different strings with whats been written in the text files :(
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: Login using text files as a database

Posted 17 July 2011 - 03:32 AM

Delimiter version.

Using sr As New StreamReader("path")
 While not sr.EndOfStream
   Dim unpw() As String = sr.Readline.Split("|")
   'example is john|jhg123
   'our array is now unpw(0) = john
   '                 unpw(1) = jhg123
   If unp(0) = txtUsername.Text AndAlso unp(1) = txtPassword.Text Then
      'success
   End If
 End While
End Using


Check your file to see how they loaded the data. It should have been:
txtUsername.Text & "|" & txtPassword.Text for each new user.
Was This Post Helpful? 1
  • +
  • -

#7 lodman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-July 11

Re: Login using text files as a database

Posted 22 July 2011 - 01:56 AM

Hey, thank you so much. This works great!

My program is up to 700ish lines now. So I've just built on the code you gave me and added more features to help the user log in and stuff. They can also register their details which works perfectly.

There's just one more thing, is it possible to edit a line?
Like the user registers their details, but then later they want to change their password or something. Is there a way to delete the specific line that held their old information?
Or maybe overwrite it?

Because at the moment It's just appending a new line to the end of the file and then there ends up being 2 copies of slightly different details pretty much. Not really useful :P

Anyway, sorry to trouble you again. But you're really helping. It's great to finally have a reliable source to learn from.

Here is the current code that i'm using, for the section that I'm talking about. The bulk of it is irrelevant to my question, but i just want to show you what i've learned.


       If System.IO.File.Exists("UserDetails.txt") = True Then

            If txtPassword.Text = "" Or txtPassword.TextLength < 6 Then
                PasswordFlag = True
                NoPassword = vbNewLine & "-Your password must be at least 6 characters"
            Else
                PasswordFlag = False
                NoPassword = ""
            End If

            If txtUsername.Text = "" Then
                FirstnameFlag = True
                NoFirstname = vbNewLine & "-First Name"
            Else
                FirstnameFlag = False
                NoFirstname = ""
            End If

            If txtLastName.Text = "" Then
                LastnameFlag = True
                NoLastname = vbNewLine & "-Last name"
            Else
                LastnameFlag = False
                NoLastname = ""
            End If

            If txtAddress.Text = "" Then
                AddressFlag = True
                NoAddress = vbNewLine & "-Address"
            Else
                AddressFlag = False
                NoAddress = ""
            End If

            If txtEmail.Text = "" Then
                EmailFlag = True
                NoEmail = vbNewLine & "-Email"
            Else
                EmailFlag = False
                NoEmail = ""
            End If

            If txtContNumber.Text = "" Then
                ContNumberFlag = True
                NoContNumber = vbNewLine & "-Contact number"
            Else
                ContNumberFlag = False
                NoContNumber = ""
            End If

            If UsernameFlag Or PasswordFlag Or FirstnameFlag Or LastnameFlag Or AddressFlag Or EmailFlag Or ContNumberFlag = True Then

                DetailsEditFlag = False
                MsgBox("Sorry, your account details cannot be changed until the following areas are filled or fixed;" & vbNewLine & _
                       NoPassword & _
                       NoFirstname & _
                       NoLastname & _
                       NoAddress & _
                       NoEmail & _
                       NoContNumber & _
                       vbNewLine & vbNewLine & "Thank you!")
            Else
                DetailsEditFlag = True
            End If
            If DetailsEditFlag = True Then
                'Write the data
                Using sw As New IO.StreamWriter("UserDetails.txt", True)
                    sw.Write(Username & "|" & Password & "|" & FirstName & "|" & LastName & "|" & Email & "|" & Address & "|" & ContNumber & vbNewLine)
                    MsgBox("Your details have been changed!")
                    Me.Close()
                    Homepage.Show()
                End Using
            End If
        Else
            MsgBox("File not found")
        End If






Thanks

This post has been edited by lodman: 22 July 2011 - 01:57 AM

Was This Post Helpful? 1
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: Login using text files as a database

Posted 22 July 2011 - 07:19 AM

That's why I would use an XML file or database, but yes it can:

Here is the code for removing a user, see what you can come up with to edit a user.
RemoveUser(username & "|" & password)

Private Sub RemoveUser(unpw As String)
  Dim list As New List(Of String)
  'load the list - temporary storage
  Using sr As New StreamReader(<filepath>)
      While Not sr.EndOfStream
        list.Add(sr.ReadLine)
      End While
  End Using
  'search for user
  For Each s As String In list
      If s.Equals(unpw) Then
        list.Remove(s)
        Exit For
      End If
  Next
  'write all data back to file - not appending
  Using sw As New StreamWriter(<filepath>)
      For Each s As String In list
        sw.WriteLine(s)
      Next
  End Using
End Sub

This post has been edited by hawkvalley1: 22 July 2011 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#9 Shiggsy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 14-July 11

Re: Login using text files as a database

Posted 22 July 2011 - 12:49 PM

All this new fangled code makes me feel so outdated :(

I Write / Read from files the old school way, loading it all into an array and work from that. For example (very simple):
Dim I, Tot as Integer
Dim MyArray(999,2) as String

Public Sub ReadFile()
FileOpen(1, "Path", Openmode.Input)
Input(1,Tot) ' This is the total "record" count
' Loop Grab info
For I = 1 to Tot
Input(1,MyArray(I,1) 'Username
Input(1,MyArray(I,2) 'Password
Next I
FileClose(1)
End Sub

Public Sub WriteFile()
FileOpen(1, "Path", Openmode.Output)
' Here we Loop write the contents of MyArray
For I = 1 to Tot
Writeline(1,MyArray(I,1),MyArray(I,2))
Next I
FileClose(1)
End Sub

Public Sub CheckUser()
Dim Username as String = Textbox1.text
Dim Password as String = Textbox2.text

For I = 1 to Tot
If Username = MyArray(I,1) then
' We have a match, so check password
If Password = MyArray(I,2) then
' Complete Match
else
'Wrong Password
Label1.text = "Incorrect Username or Password"
End If
else
' Username not found
Label1.text = "Incorrect Username or Password"
End If
End Sub

Public Sub AddUser()
Dim Username as String = Textbox1.text
Dim Password as String = TextBox2.text
Tot += 1 
MyArray(Tot,1) = Username : MyArray(Tot,2) = Password
WriteFile()
End Sub

' The File will look something like this:
' 2
' "User 1", "Pass 1"
' "User 2", "Pass 2"

' ReadFile() in Form_Load Event
' WriteFile() For any Changes, or store in memory until Form_Close Event



I know it's not the shortest of code, but its simple, easy to understand and does the job.
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: Login using text files as a database

Posted 22 July 2011 - 01:06 PM

Nothing wrong with learning something new! I am always trying to improve my code or techniques.
Was This Post Helpful? 0
  • +
  • -

#11 Richie Kael  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-October 12

Re: Login using text files as a database

Posted 08 October 2012 - 05:01 AM

View Postlodman, on 15 July 2011 - 06:45 PM, said:

Hi, im having trouble with my programme which is a login and register system using text files to store the details of the users.

How it works is the user registers their details on one form which will append those details to the end of the 2 text files that are holding the data.
At the moment this hasn't been implemented so we're just doing this manually.
Anyway, after that the user can "login" inputting a user name and password into 2 text boxes and the program looks for those details in the text files that they just registered.
I want the programme to read this by using "readline" in 2 text files and comparing what they've written in the text box with the corresponding text file and then checking whether those 2 words are on corresponding lines in the different text files, and so ensuring the user has put in the right username and password.

As I have average knowledge of vb.net and reading and writing to text files is new to me, I haven't really been able to debug it properly. But I had one idea to get the program to add an integer value at the beginning of each line and then get the program just to read the first character of each line and check if that was the same. But then I realised It ended up not having anything to do with the text boxes and I didn't know how to compare line by line anyway so It was pretty much pointless, but I'll include it in the code anyway.

On the form side there's just 2 text boxes "txtUsername" and "txtPassword" and a button for logging in "btnLogin"

  Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        Dim Usernamereader As String = "G:\Text1.txt"
        Dim PasswordReader As String = "G:\Text2.txt"
        Dim TextLineUser As String
        Dim TextLinePass As String
        Dim CountUser As Integer
        Dim CountPass As Integer
        Dim CharUser As Char
        Dim CharPass As Char

        If System.IO.File.Exists(Usernamereader) And System.IO.File.Exists(PasswordReader) = True Then

            Dim objReaderUser As New System.IO.StreamReader(Usernamereader)
            Dim objReaderPass As New System.IO.StreamReader(PasswordReader)

            Do While objReaderUser.Peek() <> -1
                CountUser = 0
                For CountUser = 1 To 10
                    CountUser = CountUser + 1
                    TextLineUser = TextLineUser & CountUser - 1 & _
                        objReaderUser.ReadLine() & vbNewLine
                Next
                CharUser = GetChar(TextLineUser, 1)
            Loop

            Do While objReaderPass.Peek() <> -1
                CountPass = 0
                For CountPass = 1 To 10
                    CountPass = CountPass + 1
                    TextLinePass = TextLinePass & CountPass - 1 & _
                        objReaderPass.ReadLine() & vbNewLine
                Next
                CharPass = GetChar(TextLinePass, 1)
            Loop

            If txtUsername.Text = TextLineUser And txtPassword.Text = TextLinePass Then
                MsgBox("you logged in!")
                MsgBox(TextLineUser)
                MsgBox(TextLinePass)
                MsgBox(CountPass)
                MsgBox(CountUser)

            Else
                MsgBox("not good")
                MsgBox(TextLineUser)
                MsgBox(TextLinePass)
                MsgBox(CountPass)
                MsgBox(CountUser)

            End If

        Else

            MsgBox("file does not exist")

        End If  


Lodman, can you please send me the completed code...I really need this for my assignment and im having real trouble at it...I appreciate you help ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1