private void btnRemoveCategory_Click(object sender, EventArgs e) { DialogResult answer = MessageBox.Show("You are about to delete the " + categoryNameComboBox.Text + " category and all of its contents. OK?", "Delete Category", MessageBoxButtons.OKCancel); if (answer == DialogResult.Cancel) { MessageBox.Show("Nothing deleted", "Delete Category"); } else { // Store category name for use in the message box later String delName = categoryNameComboBox.Text; // Store CategoryID for cleaner code int catID = Convert.ToInt32(categoryNameComboBox.SelectedValue); // Create an array of items that have the CategoryID to be deleted websitesDataSet.SiteRow[] sites = (websitesDataSet.SiteRow[])websitesDataSet.Site.Select("CategoryID = " + catID); // Go through each website and delete it foreach (websitesDataSet.SiteRow delSite in sites) { delSite.Delete(); } siteTableAdapter.Update(websitesDataSet); // Delete the Category // Search for the record by CategoryID and assign this record to a CategoryRow object websitesDataSet.CategoryRow delCat = websitesDataSet.Category.FindByCategoryID(catID); delCat.Delete(); categoryTableAdapter.Update(delCat); FilterData(); MessageBox.Show(delName + " category deleted", "Delete Category"); } }
private void btnAddCategory_Click(object sender, EventArgs e) { // Check to see if the user entered anything in the Category text box if (txtCategory.Text.Equals("")) { MessageBox.Show("You must first enter a category name in the text box!", "Add Category"); } else { // Create an object for the new category row websitesDataSet.CategoryRow newCat = websitesDataSet.Category.NewCategoryRow(); // Set the name of this category newCat.CategoryName = txtCategory.Text; // Add the new category to the data set websitesDataSet.Category.AddCategoryRow(newCat); // Update the database to reflect this new record categoryTableAdapter.Update(newCat); // Cleanup MessageBox.Show(txtCategory.Text + " category was added.", "Add Category"); txtCategory.Text = ""; // Refill the TableAdapter categoryTableAdapter.Fill(websitesDataSet.Category); } }