private void btnSave_Click(object sender, EventArgs e)
        {
            category=new Category();
            category.CategoryID = categoryID;
            category.Name = txtCategoryName.Text;
            category.CompanyID = Convert.ToInt32(companyNameComboBox.SelectedValue);
            if (txtCategoryName.Text == "")
            {
                MessageBox.Show("Please enter Category name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCategoryName.Focus();
                return;
            }

            if (companyNameComboBox.SelectedIndex==0)
            {
                MessageBox.Show("Please Select  Category name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                companyNameComboBox.Focus();
                return;
            }
            try
            {
                if (categoryGateway.CheckCategory(category))
                {
                    MessageBox.Show(@"PersonType Name Already Exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCategoryName.Focus();
                    return;
                }
                else if (btnSave.Text == @"Save")
                {
                    if (categoryGateway.AddCategory(category))
                    {
                        txtCategoryName.Text = string.Empty;
                        companyNameComboBox.SelectedIndex = 0;
                        MessageBox.Show(@"Successfully saved.", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else if (btnSave.Text == @"Update")
                {
                    if (categoryGateway.UpdateCategory(category))
                    {
                        txtCategoryName.Text = string.Empty;
                        MessageBox.Show(@"Successfully updated.", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                        return;
                    }
                }
                else
                {
                    MessageBox.Show(@"Problem to save PersonType....", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCategoryName.Text = "";
                    txtCategoryName.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public frmCategory(Category aCategory)
            : this()
        {
            try
            {
                btnSave.Text = @"Update";
                FillFieldsWith(aCategory);
                categoryID = aCategory.CategoryID;
            }
            catch (Exception exception)
            {

                MessageBox.Show(exception.Message);
            }
        }
 public bool AddCategory(Category category)
 {
     try
     {
         using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
         {
             dataContext.Categoets.Add(category);
             dataContext.SaveChanges();
             return true;
         }
     }
     catch (Exception exception)
     {
         throw new Exception(exception.Message);
     }
 }
 public bool DeleteCategory(Category category)
 {
     try
     {
         using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
         {
             dataContext.Categoets.Attach(category);
             dataContext.Entry(category).State = EntityState.Deleted;
             dataContext.SaveChanges();
             return true;
         }
     }
     catch (Exception exception)
     {
         throw new Exception(exception.Message);
     }
 }
 public bool CheckCategory(Category category)
 {
     try
     {
         using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
         {
             Category aCompany = dataContext.Categoets.FirstOrDefault(b => b.Name == category.Name && b.CompanyID==category.CompanyID);
             if (aCompany == null)
             {
                 return false;
             }
             return true;
         }
     }
     catch (Exception exception)
     {
         throw new Exception(exception.Message);
     }
 }
 private void FillFieldsWith(Category aCategory)
 {
     txtCategoryName.Text = aCategory.Name;
     companyNameComboBox.SelectedValue = aCategory.CompanyID;
 }