private void BtnSubSave_Click(object sender, EventArgs e)
        {
            if (txbSubID.TextLength > 0 & txbSubName.TextLength > 0)
            {
                Category cats;
                if (comboDictionary.TryGetValue(cbCatName.SelectedIndex, out cats))
                {
                    Subcategory new_subcat = new Subcategory(cats, txbSubID.Text, txbSubName.Text);
                    try
                    {
                        if (data_mode_sub == 1)
                        {
                            if (manager.AddNewSubcategory(new_subcat))
                            {
                                MessageBox.Show("Add new subcategory succeed!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                        else if (data_mode_sub == 2)
                        {
                            if (manager.UpdateSubcategory(new_subcat))
                            {
                                MessageBox.Show("Update subcategory succeed!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    LoadViewSubcategories();
                }
            }
            else
            {
                MessageBox.Show("Subcategory ID and Name can't be empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbSubID.Focus();
            }
        }
예제 #2
0
        public bool DeleteSubcategory(Subcategory subcat)
        {
            bool result = false;

            string myDeleteQuery = "DELETE FROM master_subcategory WHERE id='" + subcat.subcategory_id +
                                   "' AND category='" + subcat.parent.category_id + "';";
            MySqlCommand myCommand = new MySqlCommand(myDeleteQuery, dbConn);

            try
            {
                int rows = myCommand.ExecuteNonQuery();
                if (rows > 0)
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error delete master_subcategory! " + ex.Message);
            }

            return(result);
        }
예제 #3
0
        public bool AddNewSubcategory(Subcategory subcat)
        {
            bool result = false;

            string myInsertQuery = "INSERT INTO master_subcategory (id, category, nama) VALUES ('" +
                                   subcat.subcategory_id + "','" + subcat.parent.category_id + "','" + subcat.subcategory_name + "');";
            MySqlCommand myCommand = new MySqlCommand(myInsertQuery, dbConn);

            try
            {
                int rows = myCommand.ExecuteNonQuery();
                if (rows > 0)
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error insert into master_subcategory! " + ex.Message);
            }

            return(result);
        }
        private void BtnSubDel_Click(object sender, EventArgs e)
        {
            if (dgvSubcat.SelectedRows.Count > 0 && cbCatName.SelectedIndex > 0)
            {
                gbSubCat.Enabled = true;
                DataGridViewRow row = dgvSubcat.SelectedRows[0];
                Category        parent;
                comboDictionary.TryGetValue(cbCatName.SelectedIndex, out parent);

                Subcategory sel_cat = new Subcategory(parent, (string)row.Cells[0].Value, (string)row.Cells[2].Value);
                if (MessageBox.Show("Are you sure want to delete ID: " + sel_cat.subcategory_id + "?",
                                    "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        if (manager.DeleteSubcategory(sel_cat))
                        {
                            MessageBox.Show("Subcategory has been deleted!");
                        }
                        else
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                LoadViewSubcategories();
            }
            else
            {
                MessageBox.Show("Please select subcategory item first!");
            }
        }