Reputation: 127
From the input I got from textbox1,textbox2 I want to check with DB and if the given user name and password combination is available, then I wanted to update the password with the input available in textbox3. Following is my code but it seems that my update statement is not working properly, please help me to correct it...
protected void Button1_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("server=localhost; database=e-learningsystem; uid=root; password=123;port=3307;");
connection.Open();
try
{
MySqlCommand cmd1 = new MySqlCommand("UPDATE lecturer Set Password= '" + TextBox3.Text + "' WHERE UserName='" + TextBox1.Text + "' AND Password='" + TextBox2.Text + "'", connection);
cmd1.ExecuteNonQuery();
if (cmd1.ExecuteNonQuery() > 0)
{
Response.Write(@"<script language='javascript'>alert('Your Password Has Been Changed successfully!, Congratulations!')</script>");
}
else
{
Response.Write(@"<script language='javascript'>alert('Your Password Has Not Been Changed successfully!, Sorry!')</script>");
}
connection.Close();
}
catch (Exception ex)
{
Response.Write(@"<script language='javascript'>alert(ex.Message)</script>");
}
}
Upvotes: 2
Views: 132
Reputation: 8521
You are trying to update password twice by calling ExecuteNonQuery
twice. In first call to ExecuteNonQuery
it would return 1
which is not collected and when second time when you call ExecuteNonQuery
it would return 0
, so it will always execure the else
part.
Make following changes -
int updateCount = cmd1.ExecuteNonQuery();
if (updateCount > 0)
{
Response.Write(@"<script language='javascript'>alert('Your Password Has Been Changed successfully!, Congratulations!')</script>");
}
else
{
Response.Write(@"<script language='javascript'>alert('Your Password Has Not Been Changed successfully!, Sorry!')</script>");
}
Upvotes: 2