//public bool AddNewRecord(Hr_BeginEmpReceivableDues_Dtls Obj_Dtls) // { // StackFrame stackFrame = new StackFrame(); // MethodBase methodBase = stackFrame.GetMethod(); // var strErrorMessage = string.Empty; // // ObjWorkFlow_HdrDL.InsUser = "******"; // // ObjCommTransHeaderDL.InsDate = DateTime.Now.ToString("dd/MM/yyyy"); // bool result = true; // //using (System.Data.Entity.DbContextTransaction dbTran = objPharmaEntities.Database.BeginTransaction()) // //{ // try // { // if (objPharmaEntities.Database.Connection.State == System.Data.ConnectionState.Closed) // { // objPharmaEntities.Database.Connection.Open(); // } // if (Obj_Dtls != null) // { // Hr_BeginEmpReceivableDues_Dtls loclDtls = new Hr_BeginEmpReceivableDues_Dtls // { // Hdr_Id = Obj_Dtls.Hdr_Id, // Branch_Id = Obj_Dtls.Branch_Id, // Company_Id = Obj_Dtls.Company_Id, // Month_No = Obj_Dtls.Month_No, // Emp_Serial_No = Obj_Dtls.Emp_Serial_No, // TransDate = Obj_Dtls.TransDate, // HireItem_Id = Obj_Dtls.HireItem_Id, // HireItem_Value = Obj_Dtls.HireItem_Value, // TransItemDate = Obj_Dtls.TransItemDate, // DueDate = Obj_Dtls.DueDate, // RecRequestHdr_Id = Obj_Dtls.RecRequestHdr_Id, // InsItemDate = DateTime.Now, // InsItemUser = Obj_Dtls.InsItemUser // }; // objPharmaEntities.Hr_BeginEmpReceivableDues_Dtls.Add(loclDtls); // //saves all above operations within one transaction // // objPharmaEntities.SaveChanges(); // // dbTran.Commit(); // // } // } // //} // //commit transaction // // dbTran.Commit(); // } // 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; // // } // } public bool AddNewRecordByContext(Hr_BeginEmpReceivableDues_Dtls Obj_Dtls, AthelHREntities varContext) { StackFrame stackFrame = new StackFrame(); MethodBase methodBase = stackFrame.GetMethod(); var strErrorMessage = string.Empty; // ObjWorkFlow_HdrDL.InsUser = "******"; // ObjCommTransHeaderDL.InsDate = DateTime.Now.ToString("dd/MM/yyyy"); bool result = true; //using (System.Data.Entity.DbContextTransaction dbTran = objPharmaEntities.Database.BeginTransaction()) //{ try { //if (objPharmaEntities.Database.Connection.State == System.Data.ConnectionState.Closed) //{ // objPharmaEntities.Database.Connection.Open(); //} if (Obj_Dtls != null) { Hr_BeginEmpReceivableDues_Dtls loclDtls = new Hr_BeginEmpReceivableDues_Dtls { Hdr_Id = Obj_Dtls.Hdr_Id, Branch_Id = Obj_Dtls.Branch_Id, Company_Id = Obj_Dtls.Company_Id, Month_No = Obj_Dtls.Month_No,//(Obj_Dtls.Month_No).Substring(0,6), Emp_Serial_No = Obj_Dtls.Emp_Serial_No, TransDate = Obj_Dtls.TransDate, HireItem_Id = Obj_Dtls.HireItem_Id, HireItem_Value = Obj_Dtls.HireItem_Value, TransItemDate = Obj_Dtls.TransItemDate, DueDate = Obj_Dtls.DueDate, RecRequestHdr_Id = Obj_Dtls.RecRequestHdr_Id, InsItemDate = DateTime.Now, InsItemUser = Obj_Dtls.InsItemUser }; varContext.Hr_BeginEmpReceivableDues_Dtls.Add(loclDtls); varContext.SaveChanges(); //saves all above operations within one transaction // objPharmaEntities.SaveChanges(); // dbTran.Commit(); // } } //} //commit transaction // dbTran.Commit(); } 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 { // varContext.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); // } }
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); }