public bool SaveData(List <EditEmpBorrowDetailsDL> objList) { StackFrame stackFrame = new StackFrame(); MethodBase methodBase = stackFrame.GetMethod(); try { OpenEntityConnection(); GetNewIdAndRow(); int Result = 0; foreach (var obj in objList) { if (obj.Borrow_Month_Status == 6 || obj.Borrow_Month_Status == 5) { Hr_EmpBorrow_Dtls ObjForUpdate = (from objLinq in objPharmaEntities.Hr_EmpBorrow_Dtls where objLinq.Dtls_Id == obj.Dtls_Id select objLinq).FirstOrDefault(); if (ObjForUpdate != null && obj.Borrow_Month_Status == 6) { ObjForUpdate.Borrow_Month_Value = obj.Borrow_Month_Value; ObjForUpdate.Borrow_Month_Status = obj.Borrow_Month_Status; // List<string> deletion = objPharmaEntities.Database.SqlQuery<List<string>>("DELETE FROM Hr_HiringEmpDuesRequestEffect WHERE Request_Dtls_Id='"+ ObjForUpdate .Dtls_Id +"';").FirstOrDefault(); Result = objPharmaEntities.SaveChanges(); } if (ObjForUpdate != null && obj.Borrow_Month_Status == 5) { ObjForUpdate.Borrow_Month_Value = obj.Borrow_Month_Value; ObjForUpdate.Borrow_Month_Status = obj.Borrow_Month_Status; Hr_HiringEmpDuesRequestEffect ObjForUpdateRequestEffect = (from objLinq in objPharmaEntities.Hr_HiringEmpDuesRequestEffect where objLinq.Request_Dtls_Id == ObjForUpdate.Dtls_Id select objLinq).FirstOrDefault(); if (ObjForUpdateRequestEffect != null) { ObjForUpdateRequestEffect.HireItem_Value = obj.Borrow_Month_Value; } Result = objPharmaEntities.SaveChanges(); } } else if (obj.Borrow_Month_Status == 2 && obj.Dtls_Id == Guid.Empty) { Hr_EmpBorrow_Dtls newobj = new Hr_EmpBorrow_Dtls(); newobj.Dtls_Id = maxId; newobj.Borrow_Month_No = obj.Borrow_Month_No; newobj.Borrow_Month_Status = obj.Borrow_Month_Status; newobj.Borrow_Month_Value = obj.Borrow_Month_Value; newobj.Branch_Id = obj.Branch_Id; newobj.Company_Id = obj.Company_Id; newobj.Hdr_Id = obj.Hdr_Id; newobj.RowId = Convert.ToByte(MaxRowId + 1); objPharmaEntities.Hr_EmpBorrow_Dtls.Add(newobj); Result = objPharmaEntities.SaveChanges(); GetNewIdAndRow(); } else { } } return(Result > 0); } catch (Exception ex) { catchEntityvalidation((System.Data.Entity.Validation.DbEntityValidationException)ex, System.Runtime.InteropServices.Marshal.GetExceptionCode().ToString(), this.UserNameProperty.ToString(), this.GetType().Name.ToString(), methodBase.Name.ToString()); ex.InnerException.Message.ToString(); return(false); } finally { CloseEntityConnection(); } }
public bool AddBeginBalanceForBorrow(Hr_EmpBorrow_Hdr ObjHr_EmpBorrow_HdrDL, List <Hr_EmpBorrow_Dtls> ListHr_EmpBorrow_Dtls, string HireItem, decimal RecievedValue) { StackFrame stackFrame = new StackFrame(); MethodBase methodBase = stackFrame.GetMethod(); var strErrorMessage = string.Empty; // ObjHr_EmpBorrow_HdrDL.InsUser = "******"; ObjHr_EmpBorrow_HdrDL.InsDate = DateTime.Now; // ObjCommTransHeaderDL.InsDate = DateTime.Now.ToString("dd/MM/yyyy"); bool result = true; try { int resultInsertEmployee = 0; Guid StrHdr_Id; StrHdr_Id = GetNewHeaderId(); using (AthelHREntities varcontext = new AthelHREntities()) { using (var dbContextTransaction = varcontext.Database.BeginTransaction()) { try { if (varcontext.Database.Connection.State == System.Data.ConnectionState.Closed) { varcontext.Database.Connection.Open(); } //OpenEntityConnection(); Hr_EmpBorrow_Hdr loclHr_EmpBorrow_Hdr = new Hr_EmpBorrow_Hdr { Hdr_Id = StrHdr_Id, Company_Id = ObjHr_EmpBorrow_HdrDL.Company_Id, Branch_Id = ObjHr_EmpBorrow_HdrDL.Branch_Id, Emp_Serial_No = ObjHr_EmpBorrow_HdrDL.Emp_Serial_No, Transdate = ObjHr_EmpBorrow_HdrDL.Transdate, Rec_Order_No = "0", Rec_Order_HdrId = StrHdr_Id, Request_Id = ObjHr_EmpBorrow_HdrDL.Request_Id, Borrow_Value = ObjHr_EmpBorrow_HdrDL.Borrow_Value, Borrow_Period = ObjHr_EmpBorrow_HdrDL.Borrow_Period, BorrowStartDate = ObjHr_EmpBorrow_HdrDL.BorrowStartDate, Borrow_MonthValue = ObjHr_EmpBorrow_HdrDL.Borrow_MonthValue, Borrow_Status = ObjHr_EmpBorrow_HdrDL.Borrow_Status, Notes = ObjHr_EmpBorrow_HdrDL.Notes, HireItem_Id = ObjHr_EmpBorrow_HdrDL.HireItem_Id, DocumentPath = ObjHr_EmpBorrow_HdrDL.DocumentPath, Commissioner_Serial_no = ObjHr_EmpBorrow_HdrDL.Commissioner_Serial_no, BorrowDuesDate = ObjHr_EmpBorrow_HdrDL.BorrowDuesDate, MessageNotesForEmp = ObjHr_EmpBorrow_HdrDL.MessageNotesForEmp, InsUser = ObjHr_EmpBorrow_HdrDL.InsUser, InsDate = ObjHr_EmpBorrow_HdrDL.InsDate }; varcontext.Hr_EmpBorrow_Hdr.Add(loclHr_EmpBorrow_Hdr); //saves all above operations within one transaction int i = 0; foreach (Hr_EmpBorrow_Dtls ObjHr_EmpBorrow_Dtls in ListHr_EmpBorrow_Dtls) { if (ObjHr_EmpBorrow_Dtls != null) { Hr_EmpBorrow_Dtls loclHr_EmpBorrow_Dtls = new Hr_EmpBorrow_Dtls { Dtls_Id = GetNewHeaderId(), Hdr_Id = StrHdr_Id, Company_Id = ObjHr_EmpBorrow_HdrDL.Company_Id, Branch_Id = ObjHr_EmpBorrow_HdrDL.Branch_Id, Borrow_Month_No = ObjHr_EmpBorrow_Dtls.Borrow_Month_No, Borrow_Month_Value = ObjHr_EmpBorrow_Dtls.Borrow_Month_Value, Borrow_Month_Status = ObjHr_EmpBorrow_Dtls.Borrow_Month_Status, RowId = Byte.Parse((i + 1).ToString()) }; varcontext.Hr_EmpBorrow_Dtls.Add(loclHr_EmpBorrow_Dtls); //saves all above operations within one transaction } i = i + 1; } result = (varcontext.SaveChanges() > 0); if (result) { EmpBeginEmpReceivableDuesDAL objEmpBeginBalanc = new EmpBeginEmpReceivableDuesDAL(); Hr_BeginEmpReceivableDues_Dtls ListDtlsBeginBalance = new Hr_BeginEmpReceivableDues_Dtls { Hdr_Id = GetNewHeaderId(), Branch_Id = ObjHr_EmpBorrow_HdrDL.Branch_Id, Company_Id = ObjHr_EmpBorrow_HdrDL.Company_Id, Month_No = ListHr_EmpBorrow_Dtls[0].Borrow_Month_No, Emp_Serial_No = ObjHr_EmpBorrow_HdrDL.Emp_Serial_No, TransDate = ObjHr_EmpBorrow_HdrDL.Transdate, HireItem_Id = HireItem, HireItem_Value = RecievedValue, TransItemDate = ObjHr_EmpBorrow_HdrDL.Transdate, DueDate = ObjHr_EmpBorrow_HdrDL.BorrowDuesDate, RecRequestHdr_Id = StrHdr_Id, InsItemDate = DateTime.Now, InsItemUser = ObjHr_EmpBorrow_HdrDL.InsUser }; result = objEmpBeginBalanc.AddNewRecordByContext(ListDtlsBeginBalance, varcontext); if (result) { dbContextTransaction.Commit(); } else { dbContextTransaction.Rollback(); result = false; } } else { dbContextTransaction.Rollback(); result = false; } } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); strErrorMessage = fullErrorMessage; // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); //((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors.First().ValidationErrors.First().ErrorMessage // dbTran.Rollback(); dbContextTransaction.Rollback(); result = false; } catch (NullReferenceException ex) { dbContextTransaction.Rollback(); string.Concat("Processor Usage" + ex.Message); } //--- End Try Of Using catch (Exception ex) { dbContextTransaction.Rollback(); //Required according to MSDN article throw; //Not in MSDN article, but recommended so the exception still bubbles up } finally { varcontext.Database.Connection.Close(); dbContextTransaction.Dispose(); } //--- End catch } //--- End Using } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); strErrorMessage = fullErrorMessage; // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); //((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors.First().ValidationErrors.First().ErrorMessage // dbTran.Rollback(); result = false; } catch (Exception ex) { //Rollback transaction if exception occurs // dbTran.Rollback(); result = false; } finally { objPharmaEntities.Database.Connection.Close(); // dbTran.Dispose(); if (!string.IsNullOrEmpty(strErrorMessage)) { SaveErrorLog(System.Runtime.InteropServices.Marshal.GetExceptionCode().ToString(), strErrorMessage, this.UserNameProperty.ToString(), this.GetType().Name.ToString(), methodBase.Name.ToString()); } } return(result); }