Problem With Login

mssrivathsan

New member
Joined
Dec 21, 2011
Messages
1
Programming Experience
Beginner
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

VB.NET:
Dim MySqlConnection As MySqlConnection        MySqlConnection = New MySqlConnection()


        MySqlConnection.ConnectionString = "server=***; user id=***; password=3z4tmyd; database=***;"
        Try
            MySqlConnection.Open()
        Catch myerror As MySqlException
            MsgBox(myerror.Message)


        End Try
        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")


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

        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..
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,744
Location
Sydney, Australia
Programming Experience
10+
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