Click here to Skip to main content
14,971,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using (var scope = new TransactionScope())
{
 List<InvItemUom> itemUOM = this.getDb().Query<InvItemUom>("").ToList();
 dd.CompanyId = Convert.ToInt32(SessionData.GetSessionData(context, "CompanyId"));
 dd.UpdatedBy = SessionData.GetSessionUserId(context);
 dd.UpdatedOn = DateTime.Now;
 this.getDb().Update(dd);
 
foreach (var item in dd.ddDetails)
{
 item.DepDocumentId = dd.Id;
 this.getDb().Update(item);
}

if (dd.StatusId == (int)CommonEnum.Status.DepreciationDocument_Complete)
{
 foreach (var item in dd.ddDetails)
 {
  FaAccount faAccount = new FaAccount();
  FaDepreciation faDepreciation = new FaDepreciation()
  {
   AppreciateValue = 0,
   A_WDV = 0,
   Createdat = DateTime.Now,
   DepreciateValue = (double)item.DepreciationValue,
   FaAccountId = (int)item.FAAccountId,
   FinancialEndDate = (DateTime)dd.FinancialEndDate,
   FinancialStartDate = (DateTime)dd.FinancialStartDate,
   NetValue = (double)item.NetValue,
   OldValue = (double)item.ActualValue,
   Percentage = (double)item.Percentage,
   Period = (int)dd.Period,
   IsDeleted = 0,
   };
   getDb().Insert(faDepreciation);

//this calls the different repository and insert records in FaTransaction table.
faTransactionRepository.Transaction((int)item.FAAccountId,(DateTime)dd.FinancialEndDate,                            (int)CommonEnum.InvItemWarrantyDocumentType.FATransactionType.Depreciation, dd.Id, null, null, null, -(double)item.DepreciationValue, (int)dd.Period, 0, (int)dd.CompanyId, (int)(dd.UpdatedBy ?? dd.CreatedBy));

//this calls the different repository and update records in FaAccount table.
faAccount = faAccountRepository.getFaDetailById((int)item.FAAccountId);
faAccount.Amount = faDepreciation.NetValue;
faAccountRepository.updateRecord(faAccount);

int accountLocation = this.getDb().Query<int>("select LocationId from faaccount where isdeleted =0 and id = @0", item.FAAccountId).Single();

FAreg lastFaSummary = this.getDb().Query<FAreg>("select * from FAreg where CompanyId = @0 and CAST([YEAR] AS INT) = year(getdate()) - 1 and FaAccountId = @1", dd.CompanyId, item.FAAccountId).SingleOrDefault();

dynamic currentFaSummary = this.getDb().Query<dynamic>(@"select FaC.Name FixedAssets,FaT.FaAccountId, FaT.Period,ABS(SUM(case when FaT.Type = 1 then FaT.Amount else 0 end)) adp, ABS(SUM(CASE WHEN (FaT.Type = 3 or FaT.Type = 6) then FaT.Amount else 0 end)) cadp, ABS(SUM(case when FaT.Type = 5 then FaT.Amount else 0 end)) dcewop, ABS(SUM(case when (FaT.Type = 3 or FaT.Type = 6) and FaT.Period = @2 then 
dbo.GetCurrentDepreciation(FaT.FaAccountId,FaT.CompanyId,FaT.CreatedOn) / 
DATEDIFF(month, FaT.CreatedOn,  Convert(varchar, dbo.fnc_FiscalYear( GETDate(),37) +1 ) + '-'+ '7' +'-01')  else 0 end)) ddast, FaC.id categoryId, fac.DepreciationPercent dr
from FaCategory FaC join FaAccount fa on fa.CategoryId = FaC.id join FaTransaction FaT on FaT.FaAccountId=fa.id where FaT.Period = dbo.fnc_FiscalYear(GETDate(), @0) and FaT.CompanyId = @0 and FaT.FaAccountId = @1 group by FaC.Name,FaT.FaAccountId  ,FaT.Period, FaC.id, fac.DepreciationPercent", dd.CompanyId, item.FAAccountId, dd.Period).Single();

int gParentId = this.getDb().Query<int>(" with getallparent as (" +
select Id,ParentId, ROW_NUMBER() OVER ( PARTITION BY id ORDER BY id ) row_num" +
from FaCategory where Id = @0" +
union all " +
select C.Id,C.ParentID, row_num + 1 from FaCategory c inner join getallparent p on p.ParentId = c.Id" + 
" ) select top 1 Id from getallparent order by row_num desc", item.CategoryId).Single();

string Categoryinitials = CommonHelper.setLevel1Value(gParentId);

   FAreg fAreg = new FAreg()
   {
   YEAR = currentFaSummary.Period.ToString(),
   LEVEL1 = Categoryinitials,
   Location = accountLocation.ToString(),
   FAno = item.FAAccountId,
   SAno = 1,
   CFLB = lastFaSummary == null ? 0 : lastFaSummary.BC,
   ADP = currentFaSummary.adp,
   CADP = currentFaSummary.cadp,
   BC = (lastFaSummary == null ? 0 : lastFaSummary.BC) + currentFaSummary.adp,
   DR = currentFaSummary.dr,
   DEWOLY = lastFaSummary == null ? 0 : lastFaSummary.TDCEWO,
   DCEWOP = currentFaSummary.dcewop,
   DDAST = currentFaSummary.ddast,
   TDCEWO = (lastFaSummary == null ? 0 : lastFaSummary.TDCEWO) +  
   currentFaSummary.dcewop 
   - currentFaSummary.ddast,
   NBVEP =  (lastFaSummary == null ? 0 : lastFaSummary.BC) - (lastFaSummary ==       
   null ? 0 
   : lastFaSummary.TDCEWO) - currentFaSummary.dcewop,
   NBVEPY = (lastFaSummary == null ? 0 : lastFaSummary.NBVEP),
   CompanyId = dd.CompanyId,
   CategoryId = item.CategoryId,
   FaAccountId = item.FAAccountId,
   GparentId = gParentId
   };
fAreg.NBVEP = Math.Abs((double)fAreg.NBVEP);
getDb().Insert(fAreg);
  }
 }

scope.Complete();
}


C#
//This is FaTransaction.Transaction method code to have a look into it. 
public void Transaction(int id, DateTime date, int type, int typeId, int? invoice, int? vendor, int? material, double amount, int period, int locationId, int company, int assign, string remarks = "", int indAmount=0)
        {
            var db = this.getDb();
            FaTransaction ft = new FaTransaction();
            ft.FaAccountId = id;
            SetTransactionNo(ft);
            ft.Date = date;
            ft.InvoiceId = invoice;
            ft.VendorId = vendor;
            ft.TypeId = typeId;
            ft.Type = type;
            ft.remarks = remarks;
            ft.MaterialId = material;
            ft.Amount = amount;
            ft.Period = period;
            ft.LocationId = locationId;
            ft.CompanyId = company;
            ft.AssignTo = assign;
            ft.CreatedOn = DateTime.Now;
            ft.CreatedBy = SessionData.GetSessionUserId(context);
            db.Insert(ft);
        }


What I have tried:

I have tried to add
XML
<system.transactions>
 <machineSettings maxTimeout="00:30:00" />
</system.transactions>

But this also did not resolve the error. The above for each loop might go up to 1000 iterations to query, update and insert the records in multiple tables.
Posted
Updated 16-May-21 23:31pm

1 solution

Generally, this happens when the query takes too long, and the transaction times out.
You can extend it:
using (var scope = new TransactionScope(TransactionScopeOption.Required, 
                                   new System.TimeSpan(0, 30, 0))))
{
Which sets it to 30 minutes.
   
Comments
Hamza Jawed 2021 17-May-21 6:12am
   
I also have a Fa Account update method which redirects to the fa accounts repository and call update method which contains a transactionscope as well. Does it have any impact due to multiple transaction scopes?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900