Question Login codes not responding

Baraka_1989

Member
Joined
Mar 8, 2012
Messages
7
Programming Experience
Beginner
Hello guys, I'm really stuck! I'm making a login app where there are multi users and the usernames and passwords are stored in a mysql database
but when i implement it, nothing happens, I get no response! Please, HEEEEEEEEEELP

******************************************************************
BELOW ARE THE CODES
******************************************************************
Imports MySql.Data
Imports MySql.Data.MySqlClient


Public Class LoginForm
    Dim dbCon As MySqlConnection
    Dim strQuery As String
    Dim sqlCmd As MySqlCommand
    Dim DR As MySqlDataReader
    Dim enteredUserName, enteredPassword As String


    'a subroutine to check the validity of the user's credentials
    Public Sub AuthenticateUser()
        enteredUserName = TXBUserName.Text
        enteredPassword = TXBPassword.Text


        Try
            'prepare connection and query
            dbCon = New MySqlConnection("Server=localhost;User id=root;Password=;Database=register")
            strQuery = "SELECT username,password " & _
                       "FROM login WHERE username='" & enteredUserName & "' AND password='" & enteredPassword & "'"
            'creating the command to launch the query to the database
            sqlCmd = New MySqlCommand(strQuery, dbCon)


            'Open the db and kick off the query
            dbCon.Open()


            DR = sqlCmd.ExecuteReader




            While DR.Read()


                If enteredUserName = DR.Item("username") And enteredPassword = DR.Item("password") Then
                    ServicesAvailableForm.Show()
                    Me.Hide()
                Else
                    MsgBox("Wrong username and/or password", MsgBoxStyle.Critical, "ERROR")


                End If
            End While




            'close the connection in order to allow other users to use the database
            DR.Close()
            dbCon.Close()
        Catch ex As Exception
            MsgBox("FAILURE TO COMMUNICATE WITH THE DATABASE!" & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Information, "DATABASE CONNECTION STATUS")
        End Try


    End Sub
 
     
    Private Sub BTNLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNLogin.Click
        AuthenticateUser()
    End Sub
End Class
 
Last edited by a moderator:
Have a look at your query and think about what that's actually doing. It's retrieving the username and password values from the login table where the username and password values match the values entered by the user, right? So what will be the result of that? If the user enters valid credentials then it's going to return a single record containing the exact same values that you already have, while if the credentials are not valid it will return no records at all. You will either get one record or zero records, so what is the While loop for?

You don't need a loop because you know that there will never be more than one row. If there is a row then you proceed to check whether its fields match the user input. You already know that they do because the record wouldn't have been returned if they didn't, so what's that for?

What's going to happen if the credentials are invalid? There are no records so it won't enter the While loop so nothing will happen. What are you seeing happen? Nothing. Sounds about right.

There are various other issues with your code too. I suggest that you take a look at this thread:

WinForms Login

and follow the link it provides relating to the actual validation.
 
Back
Top