separate try-catch statements

This is a related question and i got a good explanation.  I have one more question. My code is below.I have 5 database calls to save. I have ONE try-catch statement. When the error is thrown, I dont know which method is throwing it. I have this in a TranscationScope so if any occurs, i want to roll back...so, for example, if the first 2 saves don't occur, i dont want to save the rest...i dont want to have orphaned records....

How can I wrap this in individual try-catch to capture exactly which step throw the error AND still keep the TransactionScope? doble?
using (TransactionScope scope = new TransactionScope())
        {
            try
            {
                Sign_UpData sd = new Sign_UpData();
                int businessNameId = SaveDescription(sd);
                SaveContactPerson(sd, businessNameId);
                SaveOfficeLocation(sd, businessNameId);
                SaveHCProviderInformation(sd,businessNameId);
                SaveOfficerUserName(sd, businessNameId);

                scope.Complete();
            }

            catch (TransactionAbortedException ex)
            {
                Common common = new Common();
                common.Log("New user", "Signup", string.Concat(ex.Message, "Stack Trace Starts here:", ex.StackTrace, "inner exception starts here: ", ex.InnerException ));
            }
            catch (SqlException ex)
            {
                Common common = new Common();
                common.Log("New user", "Signup", string.Concat(ex.Message, "Stack Trace Starts here", ex.StackTrace, "inner exception starts here: ", ex.InnerException));
            }



        }

Open in new window

LVL 8
CamilliaAsked:
Who is Participating?
 
Kenneth BrownResearch AssociateCommented:
In the try declare an int stepcompleted = 0;
Increment it after each call.
Check its value if an exception is called.
0
 
Kenneth BrownResearch AssociateCommented:
oops - the variable is declared in the transactionscope - not the try block, otherwise its out of scope by the time you catch the exception.
0
 
CamilliaAuthor Commented:

I dont understand this:
>>Check its value if an exception is called.

Check the value for what? And, I dont need to wrap each call in a try-catch?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.