private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (MssBox.QuestionYesNo("คุณต้องการบันทึกข้อมูลพนักงาน ใช่ หรือ ไม่ ?") == DialogResult.Yes)
                {
                    MssBox.iWaitForm(splashScreenManager1);

                    var db = new CDMSDBDataContext(Program.conStr);

                    var ChkKey = (from x in db.Employees
                                  where x.Emp_Key == EmpID
                                  select x).FirstOrDefault();

                    if (ChkKey == null)
                    {
                        using (var tran = new System.Transactions.TransactionScope())
                        {
                            var AddID = new AddressByIDCard();
                            AddID.AddrIDCard_Address_NO       = txtAddIDNo.Text;
                            AddID.AddrIDCard_Address_Moo      = txtAddIDMoo.Text;
                            AddID.AddrIDCard_Address_Soi      = txtAddIDSoi.Text;
                            AddID.AddrIDCard_Address_Street   = txtAddIDStreet.Text;
                            AddID.AddrIDCard_Address_District = Convert.ToInt32(LookAddIDDistrict.EditValue);
                            AddID.AddrIDCard_Address_Amphur   = Convert.ToInt32(LookAddIDAmphur.EditValue);
                            AddID.AddrIDCard_Address_Province = Convert.ToInt32(LookAddIDProvince.EditValue);
                            AddID.AddrIDCard_Address_ZipCode  = Convert.ToInt32(txtAddIDZipCode.Text);
                            AddID.AddrIDCard_Telelphone       = txtAddIDTel.Text;
                            AddID.AddrIDCard_Status           = 1;
                            AddID.AddrIDCard_Del = 0;
                            db.AddressByIDCards.InsertOnSubmit(AddID);
                            db.SubmitChanges();

                            var AddCon = new AddressByContact();
                            AddCon.AddrCont_Address_NO       = txtAddConNo.Text;
                            AddCon.AddrCont_Address_Moo      = txtAddConMoo.Text;
                            AddCon.AddrCont_Address_Soi      = txtAddConSoi.Text;
                            AddCon.AddrCont_Address_Street   = txtAddConStreet.Text;
                            AddCon.AddrCont_Address_District = Convert.ToInt32(LookAddConDistrict.EditValue);
                            AddCon.AddrCont_Address_Amphur   = Convert.ToInt32(LookAddConAmphur.EditValue);
                            AddCon.AddrCont_Address_Province = Convert.ToInt32(LookAddConProvince.EditValue);
                            AddCon.AddrCont_Address_ZipCode  = Convert.ToInt32(txtAddConZipCode.Text);
                            AddCon.AddrCont_Telelphone       = txtAddConTel.Text;
                            AddCon.AddrCont_Status           = 1;
                            AddCon.AddrCont_Del = 0;
                            db.AddressByContacts.InsertOnSubmit(AddCon);
                            db.SubmitChanges();

                            var AddFac = new AddressByCompany();
                            AddFac.AddrCom_Name             = txtAddFacName.Text;
                            AddFac.AddrCom_Address_NO       = txtAddFacNo.Text;
                            AddFac.AddrCom_Address_Moo      = txtAddFacMoo.Text;
                            AddFac.AddrCom_Address_Soi      = txtAddFacSoi.Text;
                            AddFac.AddrCom_Address_Street   = txtAddFacStreet.Text;
                            AddFac.AddrCom_Address_District = Convert.ToInt32(LookAddFacDistrict.EditValue);
                            AddFac.AddrCom_Address_Amphur   = Convert.ToInt32(LookAddFacAmphur.EditValue);
                            AddFac.AddrCom_Address_Province = Convert.ToInt32(LookAddFacProvince.EditValue);
                            AddFac.AddrCom_Address_ZipCode  = Convert.ToInt32(txtAddFacZipCode.Text);
                            AddFac.AddrCom_Telelphone       = txtAddFacTel.Text;
                            AddFac.AddrCom_Status           = 1;
                            AddFac.AddrCom_Del = 0;
                            db.AddressByCompanies.InsertOnSubmit(AddFac);
                            db.SubmitChanges();

                            var Emp = new Employee();
                            Emp.Birthday     = dateOfBirth.DateTime.Date;
                            Emp.StartDayWork = dateOfWork.DateTime.Date;
                            Emp.IDCard       = txtIDCard.Text;
                            //Emp.EmployeeID
                            Emp.Emp_Fname      = txtFName.Text;
                            Emp.Emp_Name       = txtFName.Text + "  " + txtSName.Text;
                            Emp.Emp_Surname    = txtSName.Text;
                            Emp.Age            = Convert.ToInt32(lbAge.Text);
                            Emp.Title_Key      = Convert.ToInt32(LookTitle.EditValue);
                            Emp.Gender_Key     = Convert.ToInt32(radioGender.EditValue);
                            Emp.Dept_Key       = Convert.ToInt32(LookDept.EditValue);
                            Emp.Sec_Key        = Convert.ToInt32(LookSec.EditValue);
                            Emp.Brn_Key        = Convert.ToInt32(LookBrn.EditValue);
                            Emp.Fact_Key       = Convert.ToInt32(LookFac.EditValue);
                            Emp.AddrIDCard_Key = AddID.AddrIDCard_Key;
                            Emp.AddrCont_Key   = AddCon.AddrCont_Key;
                            Emp.AddrCom_Key    = AddFac.AddrCom_Key;
                            Emp.Emp_REM        = memoREM.Text;
                            Emp.Emp_Status     = 1;
                            Emp.Emp_Del        = 0;
                            db.Employees.InsertOnSubmit(Emp);
                            db.SubmitChanges();

                            tran.Complete();
                        }
                    }
                    else
                    {
                        using (var tran = new System.Transactions.TransactionScope())
                        {
                            var AddID = (from x in db.AddressByIDCards
                                         where x.AddrIDCard_Key == ChkKey.AddrIDCard_Key
                                         select x).FirstOrDefault();

                            AddID.AddrIDCard_Address_NO       = txtAddIDNo.Text;
                            AddID.AddrIDCard_Address_Moo      = txtAddIDMoo.Text;
                            AddID.AddrIDCard_Address_Soi      = txtAddIDSoi.Text;
                            AddID.AddrIDCard_Address_Street   = txtAddIDStreet.Text;
                            AddID.AddrIDCard_Address_District = Convert.ToInt32(LookAddIDDistrict.EditValue);
                            AddID.AddrIDCard_Address_Amphur   = Convert.ToInt32(LookAddIDAmphur.EditValue);
                            AddID.AddrIDCard_Address_Province = Convert.ToInt32(LookAddIDProvince.EditValue);
                            AddID.AddrIDCard_Address_ZipCode  = Convert.ToInt32(txtAddIDZipCode.Text);
                            AddID.AddrIDCard_Telelphone       = txtAddIDTel.Text;
                            AddID.AddrIDCard_Status           = 1;
                            AddID.AddrIDCard_Del = 0;
                            db.SubmitChanges();

                            var AddCon = (from x in db.AddressByContacts
                                          where x.AddrCont_Key == ChkKey.AddrCont_Key
                                          select x).FirstOrDefault();

                            AddCon.AddrCont_Address_NO       = txtAddConNo.Text;
                            AddCon.AddrCont_Address_Moo      = txtAddConMoo.Text;
                            AddCon.AddrCont_Address_Soi      = txtAddConSoi.Text;
                            AddCon.AddrCont_Address_Street   = txtAddConStreet.Text;
                            AddCon.AddrCont_Address_District = Convert.ToInt32(LookAddConDistrict.EditValue);
                            AddCon.AddrCont_Address_Amphur   = Convert.ToInt32(LookAddConAmphur.EditValue);
                            AddCon.AddrCont_Address_Province = Convert.ToInt32(LookAddConProvince.EditValue);
                            AddCon.AddrCont_Address_ZipCode  = Convert.ToInt32(txtAddConZipCode.Text);
                            AddCon.AddrCont_Telelphone       = txtAddConTel.Text;
                            AddCon.AddrCont_Status           = 1;
                            AddCon.AddrCont_Del = 0;
                            db.SubmitChanges();

                            var AddFac = (from x in db.AddressByCompanies
                                          where x.AddrCom_Key == ChkKey.AddrCom_Key
                                          select x).FirstOrDefault();

                            AddFac.AddrCom_Name             = txtAddFacName.Text;
                            AddFac.AddrCom_Address_NO       = txtAddFacNo.Text;
                            AddFac.AddrCom_Address_Moo      = txtAddFacMoo.Text;
                            AddFac.AddrCom_Address_Soi      = txtAddFacSoi.Text;
                            AddFac.AddrCom_Address_Street   = txtAddFacStreet.Text;
                            AddFac.AddrCom_Address_District = Convert.ToInt32(LookAddFacDistrict.EditValue);
                            AddFac.AddrCom_Address_Amphur   = Convert.ToInt32(LookAddFacAmphur.EditValue);
                            AddFac.AddrCom_Address_Province = Convert.ToInt32(LookAddFacProvince.EditValue);
                            AddFac.AddrCom_Address_ZipCode  = Convert.ToInt32(txtAddFacZipCode.Text);
                            AddFac.AddrCom_Telelphone       = txtAddFacTel.Text;
                            AddFac.AddrCom_Status           = 1;
                            AddFac.AddrCom_Del = 0;
                            db.SubmitChanges();

                            var Emp = (from x in db.Employees
                                       where x.Emp_Key == ChkKey.Emp_Key
                                       select x).FirstOrDefault();

                            Emp.Birthday     = dateOfBirth.DateTime.Date;
                            Emp.StartDayWork = dateOfWork.DateTime.Date;
                            Emp.IDCard       = txtIDCard.Text;
                            //Emp.EmployeeID
                            Emp.Emp_Fname      = txtFName.Text;
                            Emp.Emp_Name       = txtFName.Text + "  " + txtSName.Text;
                            Emp.Emp_Surname    = txtSName.Text;
                            Emp.Age            = Convert.ToInt32(lbAge.Text);
                            Emp.Title_Key      = Convert.ToInt32(LookTitle.EditValue);
                            Emp.Gender_Key     = Convert.ToInt32(radioGender.EditValue);
                            Emp.Dept_Key       = Convert.ToInt32(LookDept.EditValue);
                            Emp.Sec_Key        = Convert.ToInt32(LookSec.EditValue);
                            Emp.Brn_Key        = Convert.ToInt32(LookBrn.EditValue);
                            Emp.Fact_Key       = Convert.ToInt32(LookFac.EditValue);
                            Emp.AddrIDCard_Key = AddID.AddrIDCard_Key;
                            Emp.AddrCont_Key   = AddCon.AddrCont_Key;
                            Emp.AddrCom_Key    = AddFac.AddrCom_Key;
                            Emp.Emp_REM        = memoREM.Text;
                            Emp.Emp_Status     = 1;
                            Emp.Emp_Del        = 0;
                            db.SubmitChanges();

                            tran.Complete();
                        }
                    }
                    MssBox.iWaitForm(splashScreenManager1);
                    MssBox.Success("คุณได้ทำการบันทึกข้อมูลพนักงานเรียบร้อยแล้ว");
                    this.Close();
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MssBox.iWaitForm(splashScreenManager1);
                MssBox.Error(ex.Message);
            }
        }
        private void btSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (!dxValidationProvider10.Validate())
                {
                    return;
                }

                if (!CheckBefore())
                {
                    return;
                }
                var db = new CDMSDBDataContext(Program.conStr);
                gv_sicklnjury.CloseEditor();
                gvSickness_Family.CloseEditor();
                var date_sv = MssBox.GetDateServer(db);

                #region Save_sickness_and_detail and Update

                MssBox.iWaitForm(splashScreenManager1);
                #region เตียมข้อมูล
                var getdatafor_sickness = (from i in db.HistorySicknesses where i.Emp_Key == this.emp_id select i).FirstOrDefault();

                var      sickn_ls      = (List <HistorySickness_SickInjury>)historySicknessSickInjuryBindingSource.DataSource;
                string[] for_del_sickn = { "0" };
                if (sickn_ls.Count > 0)
                {
                    string[] not_del_sickn = sickn_ls.Select(x => x.Htss_Key.ToString()).ToArray();
                    for_del_sickn = (from i in db.HistorySickness_SickInjuries where (i.Hts_Key == getdatafor_sickness.Hts_Key && !not_del_sickn.Contains(i.Htss_Key.ToString())) select i.Htss_Key.ToString()).ToArray();
                }

                var      family_ls   = (List <HistorySickness_Family>)historySicknessFamilyBindingSource.DataSource;
                string[] for_del_fam = { "0" };;
                if (family_ls.Count > 0)
                {
                    string[] not_del_fam = family_ls.Select(x => x.Htsf_Key.ToString()).ToArray();
                    for_del_fam = (from i in db.HistorySickness_Families where (i.Hts_Key == getdatafor_sickness.Hts_Key && !not_del_fam.Contains(i.Htsf_Key.ToString())) select i.Htsf_Key.ToString()).ToArray();
                }
                #endregion

                using (var ts = new System.Transactions.TransactionScope())
                {
                    int EHS_id = 0;
                    if (getdatafor_sickness == null)
                    {
                        //Save
                        var EHS = new HistorySickness();
                        EHS.Hts_DiseaseChronic         = Convert.ToBoolean(radioGroupDiseaseChronic2.EditValue);
                        EHS.Hts_DiseaseChronic_Specify = tbDiseaseChronic2.Text;
                        EHS.Hts_Sergery                  = Convert.ToBoolean(radioGroupSergery3.EditValue);
                        EHS.Hts_Sergery_Specify          = tbSergery3.Text;
                        EHS.Hts_Immunity                 = Convert.ToBoolean(radioGroupImmunity4.EditValue);
                        EHS.Hts_Immunity_Specify         = tbImmunity4.Text;
                        EHS.Hts_Family                   = Convert.ToBoolean(radioGroupFamily5.EditValue);
                        EHS.Hts_Family_Specify           = string.Empty;
                        EHS.Hts_Medicine                 = Convert.ToBoolean(radioGroupMedicine6.EditValue);
                        EHS.Hts_Medicine_Specify         = tbMedicine6.Text;
                        EHS.Hts_MedicineAllergic         = Convert.ToBoolean(radioGroupMedicineAllergic7.EditValue);
                        EHS.Hts_MedicineAllergic_Specify = tbMedicineAllergic7.Text;
                        EHS.Hts_Smoking                  = Convert.ToInt32(radioGroupSmoking8.EditValue);
                        EHS.Hts_Smoking_PresentCount     = Convert.ToInt32(tbPresentCount8.EditValue ?? "0");
                        EHS.Hts_Smoking_QuitYear         = Convert.ToInt32(tbQuitYear8.EditValue ?? "0");
                        EHS.Hts_Smoking_QuitMonth        = Convert.ToInt32(tbQuitMonth8.EditValue ?? "0");
                        EHS.Hts_Somking_BeforeQuitCount  = Convert.ToInt32(tbBeforeQuitCount8.EditValue ?? "0");
                        EHS.Hts_Drinking                 = Convert.ToInt32(radioGroupDrinking9.EditValue);
                        EHS.Hts_Drinking_QuitYear        = Convert.ToInt32(tbQuitYear9.EditValue ?? "0");
                        EHS.Hts_Drinking_QuitMonth       = Convert.ToInt32(tbQuitMonth9.EditValue ?? "0");
                        EHS.Hts_Addictive                = Convert.ToBoolean(radioGroupAddictive10.EditValue);
                        EHS.Hts_Addictive_Specify        = tbAddictive10.Text;
                        EHS.Hts_Others                   = tbOthers11.Text;
                        EHS.Hts_Rem            = "Remark";
                        EHS.Hts_CreateBy       = 0;
                        EHS.Hts_ModifyDateTime = date_sv;
                        EHS.Hts_ModifyBy       = 0;
                        EHS.Hts_Status         = 0;
                        EHS.Hts_Del            = 0;
                        EHS.Emp_Key            = this.emp_id;
                        db.HistorySicknesses.InsertOnSubmit(EHS);
                        db.SubmitChanges();

                        EHS_id = EHS.Hts_Key;
                    }
                    else
                    {
                        //Update
                        MssBox.iWaitForm(splashScreenManager1);
                        if (MssBox.QuestionYesNo("คุณต้องการอัพเดตข้อมูลประวัติการป่วย ใช่หรือไม่") == System.Windows.Forms.DialogResult.No)
                        {
                            return;
                        }
                        MssBox.iWaitForm(splashScreenManager1);

                        EHS_id = getdatafor_sickness.Hts_Key;

                        getdatafor_sickness.Hts_DiseaseChronic         = Convert.ToBoolean(radioGroupDiseaseChronic2.EditValue);
                        getdatafor_sickness.Hts_DiseaseChronic_Specify = tbDiseaseChronic2.Text;
                        getdatafor_sickness.Hts_Sergery                  = Convert.ToBoolean(radioGroupSergery3.EditValue);
                        getdatafor_sickness.Hts_Sergery_Specify          = tbSergery3.Text;
                        getdatafor_sickness.Hts_Immunity                 = Convert.ToBoolean(radioGroupImmunity4.EditValue);
                        getdatafor_sickness.Hts_Immunity_Specify         = tbImmunity4.Text;
                        getdatafor_sickness.Hts_Family                   = Convert.ToBoolean(radioGroupFamily5.EditValue);
                        getdatafor_sickness.Hts_Family_Specify           = string.Empty;
                        getdatafor_sickness.Hts_Medicine                 = Convert.ToBoolean(radioGroupMedicine6.EditValue);
                        getdatafor_sickness.Hts_Medicine_Specify         = tbMedicine6.Text;
                        getdatafor_sickness.Hts_MedicineAllergic         = Convert.ToBoolean(radioGroupMedicineAllergic7.EditValue);
                        getdatafor_sickness.Hts_MedicineAllergic_Specify = tbMedicineAllergic7.Text;
                        getdatafor_sickness.Hts_Smoking                  = Convert.ToInt32(radioGroupSmoking8.EditValue);
                        getdatafor_sickness.Hts_Smoking_PresentCount     = Convert.ToInt32(tbPresentCount8.EditValue ?? "0");
                        getdatafor_sickness.Hts_Smoking_QuitYear         = Convert.ToInt32(tbQuitYear8.EditValue ?? "0");
                        getdatafor_sickness.Hts_Smoking_QuitMonth        = Convert.ToInt32(tbQuitMonth8.EditValue ?? "0");
                        getdatafor_sickness.Hts_Somking_BeforeQuitCount  = Convert.ToInt32(tbBeforeQuitCount8.EditValue ?? "0");
                        getdatafor_sickness.Hts_Drinking                 = Convert.ToInt32(radioGroupDrinking9.EditValue);
                        getdatafor_sickness.Hts_Drinking_QuitYear        = Convert.ToInt32(tbQuitYear9.EditValue ?? "0");
                        getdatafor_sickness.Hts_Drinking_QuitMonth       = Convert.ToInt32(tbQuitMonth9.EditValue ?? "0");
                        getdatafor_sickness.Hts_Addictive                = Convert.ToBoolean(radioGroupAddictive10.EditValue);
                        getdatafor_sickness.Hts_Addictive_Specify        = tbAddictive10.Text;
                        getdatafor_sickness.Hts_Others                   = tbOthers11.Text;
                        getdatafor_sickness.Hts_Rem            = "Remark";
                        getdatafor_sickness.Hts_CreateBy       = 0;
                        getdatafor_sickness.Hts_ModifyDateTime = date_sv;
                        getdatafor_sickness.Hts_ModifyBy       = 0;
                        getdatafor_sickness.Hts_Status         = 0;
                        getdatafor_sickness.Hts_Del            = 0;
                        getdatafor_sickness.Emp_Key            = this.emp_id;
                        db.SubmitChanges();
                    }

                    if (sickn_ls.Count > 0)
                    {
                        var _Sicklnjury = new List <HistorySickness_SickInjury>();
                        foreach (var item in sickn_ls)
                        {
                            if (item.Htss_Key == 0)
                            {
                                var set_data = new HistorySickness_SickInjury()
                                {
                                    Htss_SicknessInjury = item.Htss_SicknessInjury,
                                    Htss_OnYear         = item.Htss_OnYear,
                                    Htss_Rem            = "Remark",
                                    Htss_CreateBy       = 0,
                                    Htss_ModifyDateTime = date_sv,
                                    Htss_ModifyBy       = 0,
                                    Htss_Status         = 0,
                                    Htss_Del            = 0,
                                    Hts_Key             = EHS_id
                                };
                                _Sicklnjury.Add(set_data);
                            }
                            else
                            {
                                var get_Update = (from i in db.HistorySickness_SickInjuries where i.Htss_Key == item.Htss_Key select i).FirstOrDefault();
                                get_Update.Htss_SicknessInjury = item.Htss_SicknessInjury;
                                get_Update.Htss_OnYear         = item.Htss_OnYear;
                                get_Update.Htss_Rem            = "Remark";
                                get_Update.Htss_CreateBy       = 0;
                                get_Update.Htss_ModifyDateTime = date_sv;
                                get_Update.Htss_ModifyBy       = 0;
                                get_Update.Htss_Status         = 0;
                                get_Update.Htss_Del            = 0;
                                db.SubmitChanges();
                            }
                        }
                        db.HistorySickness_SickInjuries.InsertAllOnSubmit(_Sicklnjury);
                    }

                    if (family_ls.Count > 0)
                    {
                        var _Family = new List <HistorySickness_Family>();
                        foreach (var item in family_ls)
                        {
                            if (item.Htsf_Key == 0)
                            {
                                var set_data = new HistorySickness_Family()
                                {
                                    Htsf_Relation       = item.Htsf_Relation,
                                    Htsf_Disease        = item.Htsf_Disease,
                                    Htsf_Rem            = "Remark",
                                    Htsf_CreateBy       = 0,
                                    Htsf_ModifyDateTime = date_sv,
                                    Htsf_ModifyBy       = 0,
                                    Htsf_Status         = 0,
                                    Htsf_Del            = 0,
                                    Hts_Key             = EHS_id
                                };
                                _Family.Add(set_data);
                            }
                            else
                            {
                                var get_Update = (from i in db.HistorySickness_Families where i.Htsf_Key == item.Htsf_Key select i).FirstOrDefault();
                                get_Update.Htsf_Relation       = item.Htsf_Relation;
                                get_Update.Htsf_Disease        = item.Htsf_Disease;
                                get_Update.Htsf_Rem            = "Remark";
                                get_Update.Htsf_CreateBy       = 0;
                                get_Update.Htsf_ModifyDateTime = date_sv;
                                get_Update.Htsf_ModifyBy       = 0;
                                get_Update.Htsf_Status         = 0;
                                get_Update.Htsf_Del            = 0;
                                db.SubmitChanges();
                            }
                        }
                        db.HistorySickness_Families.InsertAllOnSubmit(_Family);
                    }

                    #region Delete Detail sickness && family
                    var get_del_sikn = (from i in db.HistorySickness_SickInjuries where for_del_sickn.Contains(i.Htss_Key.ToString()) select i).ToList();
                    if (get_del_sikn.Count > 0)
                    {
                        db.HistorySickness_SickInjuries.DeleteAllOnSubmit(get_del_sikn);
                        db.SubmitChanges();
                    }

                    var get_del_fami = (from i in db.HistorySickness_Families where for_del_fam.Contains(i.Htsf_Key.ToString()) select i).ToList();
                    if (get_del_fami.Count > 0)
                    {
                        db.HistorySickness_Families.DeleteAllOnSubmit(get_del_fami);
                        db.SubmitChanges();
                    }
                    #endregion

                    db.SubmitChanges();
                    ts.Complete();
                    MssBox.iWaitForm(splashScreenManager1);
                    MssBox.Success();
                }
                #endregion
            }
            catch (Exception ex)
            {
                MssBox.iWaitForm(splashScreenManager1);
                MssBox.Error(ex);
            }
        }