I have C# forms application with a TextBox called ageTextBox;
After I press a button, the application should update one or more values from my MySQL database, but it doesn't.
Here's the code:
private void button2_Click(object sender, EventArgs e)
{
// this works perfectly if I put an actual number instead of @ag
string sql = "update persons set age = 30 where age = @ag";
OdbcCommand cmd = new OdbcCommand(sql, conn);
cmd.Parameters.Add("@ag", OdbcType.Int);
cmd.Parameters["@ag"].Value = ageTextBox.Text;
int aff = cmd.ExecuteNonQuery();
if (aff == 0)
MessageBox.Show("No rows afected");
else
MessageBox.Show("Everything went as planned");
}
}
It always shows "No rows affected"! (unless I enter something that's not a number, in which case, an exception is thrown)
Does anyone know what's going on?
Convert.ToInt(ageTextBox.Text)on the Value line. It's possible that MySQL is choking because you're passing it a string and not an int.