0

Trying to do a Log In system in VB using mysql as a database but its coming up as username and password not detected when they are in the database

I am trying to do a LogIn system in VB using mysql as a database but it's coming up as username and password not detected when they are in the database.

My second example of code: my register account code in which I am able to add to my database just not reading from it and log in.
(Register code and log in code are in different forms)

Log in code which doesn't work:

 Private Sub Button_login_Click(sender As Object, e As EventArgs) Handles Button_login.Click
    conn = New MySqlConnection
    conn.ConnectionString =
        "server=localhost;userid=root;password=root;database=librarydatabase"
    Dim reader As MySqlDataReader

    Try
        conn.Open()
        query = "select * from librarydatabase.users where Users= ' " & TextBox_Username.Text & " ' and Passwordhash=' " & TextBox_Password.Text & " ' "

        command = New MySqlCommand(query, conn)
        reader = command.ExecuteReader
        Dim count As Integer
        count = 0
        While reader.Read
            count = count + 1

        End While

        If count = 1 Then
            MessageBox.Show("You are logged in")

        ElseIf count > 1 Then
            MessageBox.Show("Username or password already exist")

        Else MessageBox.Show("Incorrect username or password")

        End If

        conn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        conn.Dispose()

    End Try
End Sub

My Register code which works fine and adds records to my database:

Private Sub Button_Register2_Click(sender As Object, e As EventArgs) Handles Button_Register2.Click
    conn = New MySqlConnection
    conn.ConnectionString =
        "server=localhost;userid=root;password=root;database=librarydatabase"
    Dim reader As MySqlDataReader

    Try
        conn.Open()
        query = "insert into librarydatabase.users (Users,Passwordhash) values ('" & TextBox_Username.Text & "','" & TextBox_Password.Text & "')"

        command = New MySqlCommand(query, conn)
        reader = command.ExecuteReader

        MessageBox.Show("you are registered")
        conn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        conn.Dispose()

    End Try
End Sub
Vikrant
  • 4,920
  • 17
  • 48
  • 72

0 Answers0