Reputation: 25
My Code executes successfully and it updates the table in database too, but still, it returns my specified error that it gets failed.
Here's my code :
private void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text) && !string.IsNullOrEmpty(textBox3.Text) && !string.IsNullOrEmpty(textBox4.Text) && !string.IsNullOrEmpty(comboBox1.Text) && !string.IsNullOrEmpty(comboBox2.Text))
{
if (textBox3.Text == textBox4.Text)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-TAACMGJ\SQLEXPRESS;Initial Catalog=libman;Integrated Security=True");
SqlCommand cmd = new SqlCommand("UPDATE logdat set pass=@pass WHERE uname=@uname AND sec_que=@sec_que AND sec_ans=@sec_ans AND type=@type", con);
cmd.Parameters.AddWithValue("@uname", textBox1.Text);
cmd.Parameters.AddWithValue("@sec_ans", textBox2.Text);
cmd.Parameters.AddWithValue("@pass", textBox3.Text);
cmd.Parameters.AddWithValue("@sec_que", comboBox1.Text);
cmd.Parameters.AddWithValue("@type", comboBox2.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
cmd.ExecuteNonQuery();
con.Close();
if (dt.Rows.Count > 0)
{
MessageBox.Show("Password Reset Successfull!!");
this.Close();
}
else
{
MessageBox.Show("Password Reset Failed!! Re-Enter Details.");
reset();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("Password Confirmation Failed!!");
textBox3.Text = "";
textBox4.Text = "";
}
}
else
{
MessageBox.Show("Please Fill All The Details!!");
}
}
Upvotes: 0
Views: 51
Reputation: 6965
ExecuteNonQuery
will not return any data
var count = cmd.ExecuteNonQuery();
if (count > 0) MessageBox.Show("Password Reset Successfull!!");
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.
Upvotes: 2