incorrect even if its the same data from the database
im doing the login form for my system but even if i input the correct record from my database the messagebox i created always say its incorrect.
here's the code for the button
string user = idtb.Text;
string password = ptb.Text;
using (MySqlConnection con = new MySqlConnection(connector))
{
con.Open();
string query = "SELECT firstname,usertype FROM userinformation WHERE username = @usernameee AND password = MD5(@passworddd)";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@usernameee", idtb.Text.Trim());
cmd.Parameters.AddWithValue("@passworddd", ptb.Text.Trim());
try
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string fn = reader.GetString("firstname");
string type = reader.GetString("usertype");
MessageBox.Show($"Welcome,{fn}!","Login Succesful");
if (type == "admin")
{
adminform adminform = new adminform();
adminform.Show();
}
else if (type == "banker")
{
bankerform bankerform = new bankerform();
bankerform.Show();
}
else if (type == "user")
{
userform userform = new userform();
userform.Show();
}
}
else
{
MessageBox.Show("Incorrect User Id/Password", "Login Failed");
}
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
9 Replies
have you tried setting breakpoints and stepping through the code?
I'm guessing that the problem is this?
if (reader.Read())
Maybe take a look at this:
https://learn.microsoft.com/en-us/dotnet/api/system.data.idatareader?view=net-8.0
IDataReader Interface (System.Data)
Provides a means of reading one or more forward-only streams of result sets obtained by executing a command at a data source, and is implemented by .NET data providers that access relational databases.
Ensure that your stored data does not actually have any whitespace, case sensitivity. Like said debug and compare what you have closely. Also make sure the password is stored in the MD5 format.
Try this slightly diff version of your code snippet.
ive tried this and check what u said but still not working
Can you clarify what you mean by the message box I created always says it is incorrect? You have 3 message box's in this routine.
im testing the record i registered in my database by logging in, i believe that i input the correct user and password but i still keep getting the login unsuccessful messagebox
I am just winging it here without doing my own debugging. You can give this a try.
thank for your help, i appreciate it. i finally find the error now it's because i set my password length to only 20, i need to set it to 50 and higher because im using the MD5
Glad to hear you found the issue. ❤️