Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, this is my first time posting, found a lot of good answers and hope others may benefit from the solution to this question.

It may be something trivial, but I cannot get my login form to display "Invalid username or password". I've set two accounts up on the remote MySQL server, both can log in fine however if I try misspelling the password/username or leave them blank completely, you can press the button as much as you want, it will not show any error message.

C#
private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                dbConnect.login_initialise(); //creates connection string
                dbConnect.login_Open_Connection(); //opens the connection
                string login_query = "SELECT * FROM auth WHERE username=@username AND pass=@pass";
                MySqlCommand cmd = new MySqlCommand(login_query, dbConnect.connection);
                pass = txtPassword.Text;
                hashed_pass = dbConnect.hash_value(pass); //creates a hashed value for the password
                cmd.Parameters.AddWithValue("@username", txtUsername.Text);
                cmd.Parameters.AddWithValue("@pass", hashed_pass);
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (reader.HasRows) 
                        {
                            MessageBox.Show("Welcome!");
                            this.Hide(); 
                            mainMenu mm = new mainMenu(); 
                            mm.Show(); 
                        }
                        else // if (reader.HasRows == false) so if no rows it shoud display this however is doesn't
                        {
                            MessageBox.Show("Wrong Username or Password");
                        }

                    }
                }
            }


Weird part about this, most of the research I found on this, users cannot log in no matter what they try however I can log in fine just can't get it to display the error message.

To add a bit more detail, I have created two user accounts on the DB manually and logged in as both so it is clearly reading from the DB but if mistyped "obb" instead of "bob" for the username, it will not show any error message.

Hopefully someone may spot in seconds where I am going wrong, this has been a really confusing issue.

Many thanks for all your help!

(Slightly off-topic but may be related, I have a form that adds users and it does the same thing, keep clicking the button and no error message it simply does not every try connection and inserting the values)
Posted
Updated 19-Dec-13 3:00am
v2

1 solution

i have some changing your code. try this
C#
  if( cmd.ExecuteNonQuery()>0) 
{
                         MessageBox.Show("Welcome!");
                            this.Hide(); 
                            mainMenu mm = new mainMenu(); 
                            mm.Show(); 
                        }
                        else // if (reader.HasRows == false) so if no rows it shoud display this however is doesn't
                        {
                            MessageBox.Show("Wrong Username or Password");
                        }

And also some changing in DataBase Query
C#
string login_query = "SELECT Count(*) FROM auth WHERE username=@username AND pass=@pass";


I hope this code will be worked on your side. for any query hit to reply free feel.
 
Share this answer
 
Comments
Linux Goblin 19-Dec-13 9:56am    
Hi joginder-banger, thank you for your quick reply! Sadly I am now unable to log in when using the same credentials
joginder-banger 19-Dec-13 9:58am    
why what's type of error you getting .
Linux Goblin 19-Dec-13 10:01am    
Well it is working to an extent, when using wrong credentials it brings up "Wrong Username or Password" and when I use the right credentials it also brings up "Wrong Username or Password"
joginder-banger 19-Dec-13 10:06am    
ok...here is working fine..change some in Login_Query.. string login_query = "SELECT username FROM auth WHERE username=@username AND pass=@pass";

and Also change in if( cmd.ExecuteScalar()!=null)
{
welcome msg
}
else
{
wrong Enter
}
joginder-banger 19-Dec-13 10:15am    
what's happen????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900