Hi Guys
This is the code i have used on C# to create a login form and it always prompts me with "Invalid username or password" even though my database holds the correct username and password. Please help???
private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text;
string password = textBox2.Text;
if (ValidateUserNamePassword(username, password))
{
MessageBox.Show("Login successful");
}
else
{
MessageBox.Show("Invalid user name or password");
return;
}
}
public bool ValidateUserNamePassword(string username, string password)
{
MySqlConnection cn = new MySqlConnection(@"SERVER=localhost;" + "DATABASE=clientmanagement;" + "UID=root;" + "PASSWORD=qwerty;");
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "tsp_GetUserNameAndPassword";
MySqlParameterCollection sqlParams = cmd.Parameters;
sqlParams.AddWithValue("@UserName", username);
sqlParams.AddWithValue("@Password", password);
cn.Open();
MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (dr.Read())
{
// this will return true if a row matching the username and password is found.
// this means that the user's input is valid
return true;
}
else
{
return false;
}
dr.Close();
cn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Loading
piyush sardharaPosted Apr 9, 2011, 6:07 AM
Nidhi ShahPosted Apr 9, 2011, 5:33 AM
SqlConnection
con.Open();
cmd.ExecuteNonQuery();
da.Fill(ds);
con.Close();
con = new SqlConnection(strConn);string strCmd = "select UserName, Password from Login where UserName='" + txtUserName.Text + "' and Password ='" + txtPassword.Text + "'";SqlCommand cmd = new SqlCommand(strCmd, con);SqlDataAdapter da = new SqlDataAdapter(strCmd, con);DataSet ds = new DataSet();MessageBox.Show("Login successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);Suthish NairPosted Mar 21, 2011, 12:43 PM
try...
CREATE DEFINER=`root`@`%` PROCEDURE tsp_GetUserNameAndPassword(
Another thing, do you have access to DB?
try...
VulpesPosted Mar 21, 2011, 12:34 PM
Also, there's no END though I assume this is because you've only posted an extract.
Apart from that, you are returning a row rather than a single value so your code should work.
Kamil ZahidPosted Mar 21, 2011, 12:17 PM
CREATE DEFINER=`root`@`localhost` PROCEDURE `tsp_GetUserNameAndPassword`()
BEGIN
SELECT
username, password
FROM login
WHERE
username = @UserName AND password = @Password;
VulpesPosted Mar 21, 2011, 10:34 AM
Suthish NairPosted Mar 21, 2011, 10:25 AM
Run the sql statement from procedure inside your editor window to check the output.
Put a breakpoint and check the value getting here (dr.Read()).
Also, take care of upper/lower case of characters.