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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Navneet.Net Full Stack DeveloperCommented:
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
Navneet.Net Full Stack DeveloperCommented:
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
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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
Navneet.Net Full Stack DeveloperCommented:
0
Navneet.Net Full Stack DeveloperCommented:
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
Navneet.Net Full Stack DeveloperCommented:
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
Navneet.Net Full Stack DeveloperCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.