Problem With Login


New member
Dec 21, 2011
Programming Experience
hey there guys. I have coded my own mysql login program. But I have got a problem with it. I have added the following codes in my program

Dim MySqlConnection As MySqlConnection        MySqlConnection = New MySqlConnection()

        MySqlConnection.ConnectionString = "server=***; user id=***; password=3z4tmyd; database=***;"
        Catch myerror As MySqlException

        End Try
            Dim myadapter As New MySqlDataAdapter
            Dim sqlquary = "SELECT * FROM register WHERE username = '" & Usernametextbox.Text & "' AND password = '" & Passwordtextbox.Text & "';"
            Dim command As New MySqlCommand
            command.Connection = MySqlConnection
            command.CommandText = sqlquary
            myadapter.SelectCommand = command
            Dim mydata As MySqlDataReader
            mydata = command.ExecuteReader()
            If mydata.HasRows = 0 Then
                MsgBox("Invalid user")

                MsgBox("Welcome user")
            End If
        Catch ex As Exception

        End Try

The problem with it is that, even if I enter a correct username and password it shows the "Invalid User" message. I have checked the username and password a thousand times. Please help me out in this problem..


VB.NET Forum Moderator
Staff member
Aug 17, 2004
Sydney, Australia
Programming Experience
First things first, don't use string concatenation to insert values into SQL code. Always use parameters. To learn why and how, follow the Blog link in my signature and check out my thread on ADO.NET Parameters.

As for the issue, you're about to learn a good lesson on thinking like a developer. I look at your code and the first thing I think is, if that code is not working then, instead of getting just that one record, how about I get all the records. I can then do several things:

1. Confirm that the table I'm querying actually contains records.
2. Loop through the records and look at the username and password for each.
3. Compare those values to my user input.

By looking at the values visually and comparing them in code, it's quite possible you will discover something like there's a space somewhere and the value you thought you had is not the value you actually have after all. Don't just keep executing the code you think you should over and over and wonder why it doesn't work. A developer very often has to write test code that they know will never make it into their project just to make sure that what they think is the case actually is the case.
Top Bottom