C
C#2w ago
Liean

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
greyfox
greyfox2w ago
have you tried setting breakpoints and stepping through the code? I'm guessing that the problem is this? if (reader.Read())
greyfox
greyfox2w ago
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.
glhays
glhays2w ago
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.
string user = idtb.Text.Trim();
string password = ptb.Text.Trim();

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", user);
cmd.Parameters.AddWithValue("@passworddd", password);

try
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string fn = reader.GetString("firstname");
string type = reader.GetString("usertype");
MessageBox.Show($"Welcome, {fn}!", "Login Successful");

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);
}
}
string user = idtb.Text.Trim();
string password = ptb.Text.Trim();

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", user);
cmd.Parameters.AddWithValue("@passworddd", password);

try
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string fn = reader.GetString("firstname");
string type = reader.GetString("usertype");
MessageBox.Show($"Welcome, {fn}!", "Login Successful");

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);
}
}
Liean
LieanOP2w ago
ive tried this and check what u said but still not working
glhays
glhays2w ago
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.
Liean
LieanOP2w ago
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
glhays
glhays2w ago
I am just winging it here without doing my own debugging. You can give this a try.
using System.Security.Cryptography;
using System.Text;

// Function to create MD5 hash
string CreateMD5(string input)
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);

StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}

string user = idtb.Text.Trim();
string password = ptb.Text.Trim();
string hashedPassword = CreateMD5(password);

using (MySqlConnection con = new MySqlConnection(connector))
{
con.Open();
string query = "SELECT firstname, usertype FROM userinformation WHERE username = @usernameee AND password = @passworddd";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@usernameee", user);
cmd.Parameters.AddWithValue("@passworddd", hashedPassword);

try
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string fn = reader.GetString("firstname");
string type = reader.GetString("usertype");
MessageBox.Show($"Welcome, {fn}!", "Login Successful");

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);
}
}
using System.Security.Cryptography;
using System.Text;

// Function to create MD5 hash
string CreateMD5(string input)
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);

StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}

string user = idtb.Text.Trim();
string password = ptb.Text.Trim();
string hashedPassword = CreateMD5(password);

using (MySqlConnection con = new MySqlConnection(connector))
{
con.Open();
string query = "SELECT firstname, usertype FROM userinformation WHERE username = @usernameee AND password = @passworddd";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@usernameee", user);
cmd.Parameters.AddWithValue("@passworddd", hashedPassword);

try
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string fn = reader.GetString("firstname");
string type = reader.GetString("usertype");
MessageBox.Show($"Welcome, {fn}!", "Login Successful");

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);
}
}
Liean
LieanOP2w ago
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
glhays
glhays2w ago
Glad to hear you found the issue. ❤️
Want results from more Discord servers?
Add your server