public int InsertTaskByContext(Hr_EmpEmploymentRequest objInsert, AthelHREntities varContexct)
        {
            StackFrame stackFrame = new StackFrame();
            MethodBase methodBase = stackFrame.GetMethod();

            //int result = Insert(objInsert).Result;
            //return result;

            int RowEffected = 0;

            try
            {
                if (objInsert != null)
                {
                    objInsert.Rec_Hdr_Id = objInsert.Rec_Hdr_Id;
                    objInsert.InsDate    = DateTime.Now;//DateTime.Today;
                    varContexct.Hr_EmpEmploymentRequest.Add(objInsert);
                    RowEffected = varContexct.SaveChanges();
                    //varAthelHREntities.SaveChanges();
                }
            }
            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());

                RowEffected = -1;
                ex.InnerException.Message.ToString();
            }
            finally
            {
                CloseEntityConnection();
            }
            return(RowEffected);
        }
        public async Task <int> Insert(Hr_EmpEmploymentRequest objInsert)
        {
            StackFrame stackFrame = new StackFrame();
            MethodBase methodBase = stackFrame.GetMethod();

            int RowEffected = 0;

            try
            {
                if (objInsert != null)
                {
                    OpenEntityConnection();
                    objInsert.InsDate = DateTime.Now;//DateTime.Today;


                    objPharmaEntities.Hr_EmpEmploymentRequest.Add(objInsert);
                    RowEffected = await objPharmaEntities.SaveChangesAsync();
                }
            }
            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());
                RowEffected = -1;
                ex.InnerException.Message.ToString();
            }
            finally
            {
                CloseEntityConnection();
            }
            return(RowEffected);
        }
        public Hr_EmpEmploymentRequest GetRequestByHdrId(string strCompanyNo, string strBranchNo, Guid RequestHdrId)
        {
            StackFrame stackFrame = new StackFrame();
            MethodBase methodBase = stackFrame.GetMethod();

            try
            {
                OpenEntityConnection();

                string sql = "select Rec_Hdr_Id,ReferenceNo,Request_Id,Rec_No,TransDate,InsUser,InsDate,Company_Id,Branch_Id";
                sql = sql + ",Emp_Serial_No,WithinBudget,Admin_Id,Dept_Id,Notes,DocumentPath,Order_Status,Commissioner_Serial_no";
                sql = sql + " from Hr_EmpEmploymentRequest where  Company_Id='" + strCompanyNo + "' and Branch_Id='" + strBranchNo + "' and Rec_Hdr_Id = '" + RequestHdrId + "'";


                Hr_EmpEmploymentRequest obj = objPharmaEntities.Database.SqlQuery <Hr_EmpEmploymentRequest>(sql).FirstOrDefault();
                return(obj);

                //  EmpEmploymentRequestDL objectList = new EmpEmploymentRequestDL();

                //Hr_EmpEmploymentRequest obj = (from objLinq in objPharmaEntities.Hr_EmpEmploymentRequest
                //               where objLinq.Company_Id == strCompanyNo && objLinq.Branch_Id==strBranchNo && objLinq.Rec_Hdr_Id == RequestHdrId

                //               select objLinq).FirstOrDefault();


                //                            select new
                //                            {
                //                                Rec_Hdr_Id = objLinq.Rec_Hdr_Id,
                //                                ReferenceNo = objLinq.ReferenceNo,
                //                                Request_Id = objLinq.Request_Id,
                //                                Emp_Serial_No = objLinq.Emp_Serial_No,
                //                                FromDate = objLinq.FromDate,
                //                                ToDate = objLinq.ToDate,
                //                                Notes = objLinq.Notes,
                //                                PlaceOfResidence = objLinq.PlaceOfResidence,
                //                                DocumentPath = objLinq.DocumentPath,
                //                                Order_Status = objLinq.Order_Status
                //                            });


                //Hr_EmpEmploymentRequest obj = objPharmaEntities.Hr_EmpEmploymentRequest
                //                      .Where(objLinq => objLinq.Company_Id == strCompanyNo && objLinq.Branch_Id == strBranchNo && objLinq.Rec_Hdr_Id == RequestHdrId)
                //                      .Select(
                //                         objLinq =>
                //                                new
                //                                {
                //                                    Rec_Hdr_Id = objLinq.Rec_Hdr_Id,
                //                                    ReferenceNo = objLinq.ReferenceNo,
                //                                    Request_Id = objLinq.Request_Id,
                //                                    Emp_Serial_No = objLinq.Emp_Serial_No,
                //                                    FromDate = objLinq.FromDate,
                //                                    ToDate = objLinq.ToDate,
                //                                    Notes = objLinq.Notes,
                //                                    PlaceOfResidence = objLinq.PlaceOfResidence,
                //                                    DocumentPath = objLinq.DocumentPath,
                //                                    Order_Status = objLinq.Order_Status
                //                                });

                //  return objLinq;

                //Rec_No ,ReferenceNo ,Request_Id
                //,Company_Id ,Branch_Id ,Emp_Serial_No ,TransDate ,FromDate ,ToDate ,BackDate ,Notes ,InsUser ,InsDate ,Order_Status ,PlaceOfResidence ,DocumentPath ,RequestTypeName ,StatusName
            }
            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(null);
            }
            finally
            {
                CloseEntityConnection();
            }
        }
        public bool InsetEmployeementReguest(Hr_EmpEmploymentRequest objInsert, List <Hr_EmploymentQualifyRequest> ListDtlsEmpQulify, List <Hr_EmploymentExperinceRequest> Obj_DtlsExprince)
        {
            StackFrame stackFrame      = new StackFrame();
            MethodBase methodBase      = stackFrame.GetMethod();
            bool       result          = false;
            var        strErrorMessage = string.Empty;

            try
            {
                int  resultInsertEmployee = 0;
                Guid varEmpHdrId;
                varEmpHdrId = 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();

                            EmploymentExperinceRequestDAL objEmploymentExperince = new EmploymentExperinceRequestDAL();

                            result = objEmploymentExperince.AddEmploymentExperinceRequestByContext(Obj_DtlsExprince, varEmpHdrId, UserNameProperty, varcontext);

                            if (result)
                            {
                                //EmpSpousesDAL objEmpSpouses = new EmpSpousesDAL();

                                //result = objEmpSpouses.AddEmpSpousesByContext(ListDtlsEmpSpouses, varEmpHdrId, UserNameProperty, varcontext);
                                //if (result)
                                //{
                                EmploymentQualifyRequestDAL objEmploymentQulify = new EmploymentQualifyRequestDAL();

                                result = objEmploymentQulify.AddEmploymentQualifyRequestByContext(ListDtlsEmpQulify, varEmpHdrId, UserNameProperty, varcontext);

                                if (result)
                                {
                                    EmpEmploymentRequestDAL objEmp = new EmpEmploymentRequestDAL();
                                    objInsert.Rec_Hdr_Id = varEmpHdrId;
                                    objInsert.InsUser    = UserNameProperty;
                                    resultInsertEmployee = objEmp.InsertTaskByContext(objInsert, varcontext);

                                    result = (resultInsertEmployee > 0);
                                    varcontext.SaveChanges();
                                    dbContextTransaction.Commit();
                                }
                            }
                            //}



                            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;
                        }
                        //--- 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
                }
            }

            //--- End Try


            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);
        }