How to process data between two function calls one by one

I need to use two database functions, in which I need to delete a comment, and after this second function, which reduces the user's rating.

Problem: If an exception occurs in the second function, the score will not be subtracted, but the comment will be deleted.

I am trying to save the action of the first function in a class object, and if an exception occurs in the second, the opposite action will be performed to rewrite it in the database. Is there any other approach that can be done, please suggest me. thank you in advance

+4
source share
2 answers

Within the same transaction, you must have both database calls. That way, if you get an exception, everyone will be rollback.

It depends on how you access the database, but it could be something like this:

using (var Conn = new SqlConnection(_ConnectionString)) { SqlTransaction trans = null; try { Conn.Open(); trans = Conn.BeginTransaction(); using (SqlCommand Com = new SqlCommand(ComText, Conn, trans)) { // delete comment // update score } trans.Commit(); } catch (Exception Ex) { if (trans != null) trans.Rollback(); return -1; } } 
+2
source

If both functions need to be written or copy one of them, you need to place a transaction around both functions.

If you make a transaction, the comment and reduction score will be stored in the database, if you have an exception, you can roll back and nothing happens in your database.

+1
source

All Articles