I have two tables "EnrolledSubjects" and "StudentAccounts" in SqlServer. Obviously the enrolled subjects should go to the former while their fees should go to the latter. I use the simple Insert Statement for both tables but the problem is, how can I be sure that when one of the tables fail on insert, the other table must not insert anything too. I can use try-catch like so:
try
{
insert statement to EnrolledSubjects table;
insert statement to StudentAccounts table;
} catch(Exception ex) {
get the error here
}
but what if there is no error in inserting to the EnrolledSubjects but error occurs in the insertion to the StudentAccounts? Then that means I would need to delete whatever was inserted to the EnrolledSubjects table.
Can you please tell me how to implement this right according to my specification. Thnnk you.