I want to update a row in a table:
try
{
string sql ="UPDATE TableNAme SET FirstName ='John' WHERE ID = 123";
MySqlCommand command = new MySqlCommand(sql, connection);
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception)
{
}
finally
{
connection.Close();
}
Based on the ID (key), it works perfectly if the ID was in the table, but if the ID doesn't exist in the table it shows no error message.
Is there a way that I can know if the ID was not found?
Actually ExecuteNonQuery
returns the number of rows affected. You can make use of that:
int affectedRows = command.ExecuteNonQuery();
if (affectedRows == 0)
{
// show error;
}
else
{
// success;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With