private void btnSave_Click(object sender, EventArgs e)
        {
            personType = new PersonType();
            personType.ID = typeID;
            personType.Name = txtTypeName.Text;

            if (txtTypeName.Text == "")
            {
                MessageBox.Show("Please enter personType name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTypeName.Focus();
                return;
            }
            try
            {
                if (TypeNameGateway.CheckCompany(personType))
                {
                    MessageBox.Show(@"PersonType Name Already Exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTypeName.Focus();
                    return;
                }
                else if (btnSave.Text==@"Save")
                {
                    if (TypeNameGateway.AddCompany(personType))
                    {
                        txtTypeName.Text = string.Empty;
                        MessageBox.Show(@"Successfully saved.", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else if (btnSave.Text == @"Update")
                {
                    if (TypeNameGateway.UpdateCompany(personType))
                    {
                        txtTypeName.Text = string.Empty;
                        MessageBox.Show(@"Successfully updated.", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        frmMainMenu myForm = (frmMainMenu)Application.OpenForms["frmMainMenu"];
                        frmPersonTypeRecord frmPersonTypeRecord=new frmPersonTypeRecord(){MdiParent = myForm};
                        frmPersonTypeRecord.Show();
                        this.Close();
                        return;
                    }
                }
                else
                {
                    MessageBox.Show(@"Problem to save PersonType....", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTypeName.Text = "";
                    txtTypeName.Focus();
                    return;
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 public frmPersonType(PersonType aPersonType)
     : this()
 {
     try
     {
         btnSave.Text = @"Update";
         FillFieldsWith(aPersonType);
         typeID = aPersonType.ID;
     }
     catch (Exception exception)
     {
         MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
 public bool AddCompany(PersonType company)
 {
     try
     {
         using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
         {
             dataContext.Companies.Add(company);
             dataContext.SaveChanges();
             return true;
         }
     }
     catch (Exception exception)
     {
         throw new Exception(exception.Message);
     }
 }
 public bool DeleteCompany(PersonType company)
 {
     try
     {
         using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
         {
             dataContext.Companies.Attach(company);
             dataContext.Entry(company).State=EntityState.Deleted;
             dataContext.SaveChanges();
             return true;
         }
     }
     catch (Exception exception)
     {
         throw new Exception(exception.Message);
     }
 }
 public bool CheckCompany(PersonType company)
 {
     try
     {
         using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
         {
             PersonType aCompany = dataContext.Companies.Where(b => b.Name == company.Name).FirstOrDefault();
             if (aCompany==null)
             {
                 return false;
             }
                 return true;
         }
     }
     catch (Exception exception)
     {
         throw new Exception(exception.Message);
     }
 }
 private void FillFieldsWith(PersonType updatePersonType)
 {
     txtTypeName.Text = updatePersonType.Name;
 }
        public bool UpdateCompany(PersonType company)
        {
            try
            {
                using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
                {
                    dataContext.Companies.Attach(company);
                    dataContext.Entry(company).State = EntityState.Modified;
                    dataContext.SaveChanges();
                    return true;

                    //_cnt.Users.Attach(user);
                    //_cnt.Entry<User>(user).Property(u => u.PasswordHash).IsModified = true;
                    //_cnt.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }