1

It has started confusing me.

I am using transaction Scope and the transaction is not rolling bake the insert to the database in the Fill_WO_Report_Table(..).

I am looking for specific solution to this problem because i have already researched about this topic on forums.

my code:

 public bool Generate(decimal wo_id)
 {
        string exception;
        using (var scope = new TransactionScope())
        {                
            try
            {
                if (Fill_WO_Report_Table(wo_id, out exception) == false)
                {                        
                        scope.Dispose();
                        throw new Exception("Cannot Complete Transaction.\n" + exception);
                }

. . // other code logic // other code logic . .

                if (Drop_WO_Report_Table(out exception) == false)
                    throw new Exception("Cannot Complete Transaction" + exception);


                scope.Complete();
                scope.Dispose();

                return true;
            }
            catch (Exception exe)
            {
                scope.Dispose();
                MessageBox.Show(exe.Message, "WO Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }// scope

Fill_WO_Report_Table() function is as under : -

    public bool Fill_WO_Report_Table(decimal wo_id, out string excp)
    {

        try
        {                
            if (GetCompanyDetails(ref dtCompany_Details, out excp) == false)
                throw new Exception(excp);
            if (excp != null)
            {
                MessageBox.Show("Failed to Load Company Details.\n" + excp, "WO Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }


            //query1
            functionToExecQuery();
            //query2
            functionToExecQuery();
            //query3
            functionToExecQuery();
            //query4
            functionToExecQuery();

            return true;
        }
        catch (Exception exe)
        {            
            return false;
            throw;
        }

When the exception is raised from functionToExecQuery() for Query3 it should roll back the Create or insert performed in Query1 and Query2.

How can i achieve this ?

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
Ankityp
  • 11
  • 1

0 Answers0