0

I check in various tables whether the value to be deleted exists. Everything is working correctly, but I would like to program a message that informs the user that the entry cannot be deleted because it is still present in another table. I would like to ask you for your support. This is my query:

con.Open();
using (cmd = new SqlCommand("delete from Clinic where C_ID=@C_ID AND NOT EXISTS (SELECT * FROM Document_Type AS o WHERE o.C_ID=@C_ID)", con))
{
    cmd.Parameters.AddWithValue("@C_ID", klinikUD.Value);
    cmd.ExecuteNonQuery();
    con.Close();

}
Marco
  • 19
  • 5
  • you could throw a custom exception and handle it accordingly in a part/module (of your app) that 'communicates' with the UI – vhr Jul 12 '22 at 10:50
  • 2
    `cmd.ExecuteNonQuery();` returns the number of rows affected (updated/deleted)... you should store the value in integer and check if it is 0 or not.. if it zero you should show message to the user – Chetan Jul 12 '22 at 10:51
  • @Chetan Thanks for this, it was the solution for me. – Marco Jul 12 '22 at 11:47
  • Does this answer your question? [Get affected rows on ExecuteNonQuery](https://stackoverflow.com/questions/10059158/get-affected-rows-on-executenonquery) – Charlieface Jul 12 '22 at 11:47
  • Side note: `con` and `cmd` must be in `using` to dispose them, do not cache them in global fields – Charlieface Jul 12 '22 at 11:48

0 Answers0