private async Task<int> AddEdu()
        {
            try
            {
                int tmp = Convert.ToInt16(TxtbxYear.Text);

                using (var db = new CVDBContext())
                {
                    var query = from edu in db.Educations
                                where edu.EDU_ID == IDtoshow
                                select edu;

                    if (query.Count() < 1)
                    {
                        eduToAdd = new Educations
                        {
                            Course = TxtbxCourse.Text,
                            Degree = TxtbxDegree.Text,
                            Year = Convert.ToInt16(TxtbxYear.Text),
                            Notes = TxtbxNotes.Text,
                            School = TxtbxSchool.Text
                        };
                        db.Educations.Add(eduToAdd);
                        db.Entry(eduToAdd).State = EntityState.Added;
                        await db.SaveChangesAsync();
                        eduID = eduToAdd.GetEduID();
                    }
                    else
                    {
                        eduToAdd = query.FirstOrDefault();
                        eduToAdd.Course = TxtbxCourse.Text;
                        eduToAdd.Degree = TxtbxDegree.Text;
                        eduToAdd.Year = Convert.ToInt16(TxtbxYear.Text);
                        eduToAdd.Notes = TxtbxNotes.Text;
                        eduToAdd.School = TxtbxSchool.Text;
                        eduID = eduToAdd.EDU_ID;
                        db.Entry(eduToAdd).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                }
                return eduID;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.InnerException, "add education error");
                return 0;
            }
        }
        private void BtnDeleteEdu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var db = new CVDBContext())
                {
                    var query = from rel in db.User_EDU_REL
                                where rel.User_ID == uid && rel.EDU_ID == IDtoshow
                                select rel;
                    User_EDU_REL uerToDelete = query.FirstOrDefault();
                    db.User_EDU_REL.Remove(uerToDelete);
                    db.SaveChanges();


                    //Educations eduToDelete = db.Educations.Find(IDtoshow);
                    //db.Educations.Remove(eduToDelete);
                    //db.SaveChanges();
                    FillGrdEdu();
                    ClearFields();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.InnerException, "delete user edu relation error");
            }
        }
 private async void BtnAddEdu_Click(object sender, RoutedEventArgs e)
 {
     int tmp = 0;
     try
     {
         tmp = await AddEdu();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message + ex.InnerException, "save education error");
     }
     //eduID = eduToAdd.EDU_ID;
     //eduID = eduToAdd.GetEduID();
     try
     {
         using (var db = new CVDBContext())
         {
             var query = from rel in db.User_EDU_REL
                         where rel.User_ID == uid && rel.EDU_ID == tmp
                         select rel.User_EDU_ID;
             int i = query.FirstOrDefault();
             User_EDU_REL uer = db.User_EDU_REL.Find(i);
             if (uer == null)
             {
                 uer = new User_EDU_REL
                 {
                     User_ID = uid,
                     EDU_ID = tmp
                 };
                 db.User_EDU_REL.Add(uer);
                 db.Entry(uer).State = EntityState.Added;
             }
             else
             {
                 db.Entry(uer).State = EntityState.Modified;
             }
             db.SaveChanges();
             FillGrdEdu();
             ClearFields();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message + ex.InnerException, "save user edu relationship error");
     }
 }
        private async Task<int?> Save_Ref()
        {
            //return 1;
            //int tmp = 0;
            //if (TxtbxCompany.Text.Length > 0)
            //{
            //    tmp = await SaveCompany(TxtbxCompany.Text);
            //}
            try
            {
                using (var db = new CVDBContext())
                {
                    int? tmp = null;
                    Employment_Histories e = db.Employment_Histories.Find(emp_hisID);
                    if (e != null)
                    {
                        tmp = e.REF_ID;
                        refID = e.REF_ID;
                    }
                    if (tmp < 1 || tmp == null)
                    {
                        if (TxtbxCompanyRef.Text.Length > 0 &&
                            TxtbxEmailRef.Text.Length > 0 &&
                            TxtbxFirstNameRef.Text.Length > 0 &&
                            TxtbxLastNameRef.Text.Length > 0 &&
                            TxtbxMobileRef.Text.Length > 0 &&
                            TxtbxPhoneRef.Text.Length > 0 &&
                            TxtbxTitleRef.Text.Length > 0)
                        {
                            return null;
                        }
                        else
                        {
                            User_References ur = new User_References
                            {
                                Company_ID = coID, //await SaveCompany(TxtbxCompany.Text),
                                First_Name = TxtbxFirstNameRef.Text,
                                Last_Name = TxtbxLastNameRef.Text,
                                Title = TxtbxTitleRef.Text,
                                Phone = TxtbxPhoneRef.Text,
                                Mobile = TxtbxMobileRef.Text,
                                Email = TxtbxEmailRef.Text
                            };
                            db.User_References.Add(ur);
                            db.Entry(ur).State = EntityState.Added;
                            await db.SaveChangesAsync();
                            PopupAddRef.IsOpen = false;
                            return ur.REF_ID;
                        }
                    }
                    User_References u_r = db.User_References.Find(refID);
                    u_r.First_Name = TxtbxFirstNameRef.Text;
                    u_r.Last_Name = TxtbxLastNameRef.Text;
                    u_r.Title = TxtbxTitleRef.Text;
                    u_r.Phone = TxtbxPhoneRef.Text;
                    u_r.Mobile = TxtbxMobileRef.Text;
                    u_r.Email = TxtbxEmailRef.Text;
                    db.Entry(u_r).State = EntityState.Modified;
                    db.SaveChanges();

                    return u_r.REF_ID;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.InnerException, "Save Error");
                return null;
            }
        }
 private void BtnDeleteExp_Click(object sender, RoutedEventArgs e)
 {
     ClearFields();
     try
     {
         using (var db = new CVDBContext())
         {
             var query = from exp in db.Employment_Histories
                         where exp.EMP_HIS_ID == emp_hisID
                         select exp;
             Employment_Histories ehToDelete = query.FirstOrDefault();
             db.Employment_Histories.Remove(ehToDelete);
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message + ex.InnerException, "Error");
     }
     FillGrdRef();
 }
        private async void BtnSaveExp_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                coID = await SaveCompany(TxtbxCompany.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.InnerException, "Company Error");
            }
            try
            {
                refID = await Save_Ref();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.InnerException, "Reference Error");
            }

            try
            {
                using (var db = new CVDBContext())
                {
                    if (emp_hisID < 1)
                    {
                        Employment_Histories eh = new Employment_Histories
                        {
                            Company_ID = coID,
                            User_ID = uid,
                            Department = TxtbxDepartment.Text,
                            Position = TxtbxPosition.Text,
                            Title = TxtbxTitle.Text,
                            From_Date = (DateTime)DPFrom.SelectedDate,
                            Until_Date = (DateTime?)DPUntil.SelectedDate,
                            Curr_Emp = (bool)ChkbxCurrentEMP.IsChecked ? true : false,
                            REF_ID = refID,
                            Notes = TxtbxNotes.Text
                        };
                        if ((bool)ChkbxCurrentEMP.IsChecked)
                        {
                            eh.Until_Date = null;
                        }
                        db.Employment_Histories.Add(eh);
                        db.Entry(eh).State = EntityState.Added;
                        db.SaveChanges();
                    }
                    else
                    {
                        Employment_Histories eh = db.Employment_Histories.Find(emp_hisID);
                        if (eh.REF_ID < 1 || eh.REF_ID == null)
                        {
                            int? tmp = await Save_Ref();
                            eh.REF_ID = tmp;
                        }
                        eh.Company_ID = coID;
                        eh.User_ID = uid;
                        eh.Department = TxtbxDepartment.Text;
                        eh.Position = TxtbxPosition.Text;
                        eh.Title = TxtbxTitle.Text;
                        eh.From_Date = (DateTime)DPFrom.SelectedDate;
                        eh.Until_Date = (DateTime?)DPUntil.SelectedDate;
                        eh.Curr_Emp = (bool)ChkbxCurrentEMP.IsChecked ? true : false;
                        eh.Notes = TxtbxNotes.Text;
                        if ((bool)ChkbxCurrentEMP.IsChecked)
                        {
                            eh.Until_Date = null;
                        }
                        db.Entry(eh).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.InnerException, "Save emp_his error");
            }
            ClearFields();
            FillGrdRef();
        }