Wrap each statement in a try-catch

I want to know which step causes an error in this try-catch. How can I wrap each statement in a try catch BUT still keep the transcation roll back?
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?
 
NavneetSoftware EngineerCommented:
0
 
NavneetSoftware EngineerCommented:
Hi!

Put try catch inside of every Procedures you are calling and then simple throw the exception from there

public void SaveContactPerson(sd, businessNameId)
{
try
{...}
catch(Exception ex)
{ throw;}

}
 This would bring back all required information and can be displayed and debugged.

Thanks!
0
 
CamilliaAuthor Commented:
1. Ah. Ok now, I use "throw" and how do I capture it in that SAVE I have?

2. Also, suppose the second procedure throws an error, how I roll back the first one?
                 int businessNameId = SaveDescription(sd); // this works fine
                SaveContactPerson(sd, businessNameId);  //this throws an error. How do roll back the first one?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
NavneetSoftware EngineerCommented:
Hi!

You have to pass Transaction scope as Ref SaveDescription(sd, scope);
And use this scope in your operatiion inside your SaveDescription
So that every operation inside SaveDescription is also covered under that Transaction scope

Thanks!

0
 
CamilliaAuthor Commented:
Can you give me an example? Not sure how to change my code...
and so, if saveDescription succeeds and saveContact fails..will saveDescription roll back?
0
 
NavneetSoftware EngineerCommented:
0
 
NavneetSoftware EngineerCommented:
0
 
CamilliaAuthor Commented:
thanks, let me see. I will post back if i have a question (or close this and open a related one)
0
 
NavneetSoftware EngineerCommented:
Sure!
Also if possible share SaveDescription() code

thanks!
0
 
CamilliaAuthor Commented:
This is the code. I use LINQ
public int SaveDescription(SignupDescription entity)
          {
              SignupDescription desc = new SignupDescription
                                           {
                                               BusinessName = entity.BusinessName,
                                               CateogryId = entity.CateogryId,
                                               Description =
                                                   entity.Description.Length > 1000
                                                       ? entity.Description.Substring(0, 1000)
                                                       : entity.Description,
                                               website = entity.website,
                                               OtherCategory = entity.OtherCategory,
                                               UpdateDate = entity.UpdateDate
                                           };

              this.SignupDescriptions.InsertOnSubmit(desc);
              this.SubmitChanges();
             // businessId = desc.Id; //returned to other routines

              return desc.Id;
          }

Open in new window

0
 
CamilliaAuthor Commented:
I think i need to follow this (you posted it earlier..i read the last link you sent as well)
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope(v=VS.100).aspx

Have a "return value" to see if each step is complete and if not rollback. But still dont know how to roll back since I have the Transaction around ALL of the save statements.
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.