/// <summary> /// When the user clicks the Add Electronic button, if the variable existingElec /// is null it means the electronic being added is a new one so it is added to the database. /// If it is not null, it already exists in the database and is being updated instead. /// </summary> private void addElectCmd_Click(object sender, EventArgs e) { if (existingElec == null) { Electronic electToAdd = new Electronic(); electToAdd.Name = nameTxt.Text; electToAdd.Manufacturer = manufactTxt.Text; electToAdd.Price = Convert.ToDouble(priceTxt.Text); electToAdd.Category = Validation.casingWords(categoryCbx.Text); ElectronicDb.Add(electToAdd); } else { existingElec.Name = nameTxt.Text; existingElec.Manufacturer = manufactTxt.Text; existingElec.Price = Convert.ToDouble(priceTxt.Text); existingElec.Category = Validation.casingWords(categoryCbx.Text); ElectronicDb.Update(existingElec); MessageBox.Show("Successfully updated item " + existingElec.Name); } Close(); }
/// <summary> /// Adds a new electronic into the database /// </summary> /// <param name="elect">The electronic to add to the database</param> /// <returns>Electronic that was added</returns> public static Electronic Add(Electronic elect) { using (TrackingContext context = new TrackingContext()) { context.Electronics.Add(elect); context.SaveChanges(); return(elect); } }
/// <summary> /// Updates an electronic that currently exists in the database /// </summary> /// <param name="elect">The electronic to update</param> /// <returns>Electronic that was updated</returns> public static Electronic Update(Electronic elect) { using (TrackingContext context = new TrackingContext()) { context.Entry(elect).State = EntityState.Modified; context.SaveChanges(); return(elect); } }
/// <summary> /// Accesses the database and deletes the electronic that was passed in /// </summary> /// <param name="id">The id of the electronic to delete</param> public static void Delete(int id) { using (TrackingContext context = new TrackingContext()) { Electronic electDelete = (from elect in context.Electronics where elect.ProductId == id select elect).Single(); context.Entry(electDelete).State = EntityState.Deleted; context.SaveChanges(); } }
/// <summary> /// Populates the form with the current electronic's data, if it exists. /// Then sets the variable existingElec to be the electronic that was passed in. /// </summary> /// <param name="electronic"></param> public addElectFrm(Electronic electronic) { InitializeComponent(); nameTxt.Text = electronic.Name; manufactTxt.Text = electronic.Manufacturer; priceTxt.Text = electronic.Price.ToString(); categoryCbx.Text = electronic.Category; addElectCmd.Text = "Update Electronic"; this.Text = "Update " + electronic.Name; existingElec = electronic; }
/// <summary> /// When the Update Electronic button is pressed, open up a new form /// with the information of that electronic already populated. The user may /// then update any information regarding the electronic. After closing, /// the listbox is once again repopulated. Displays an error /// message if there is no electronic selected. /// </summary> private void updateElectCmd_Click(object sender, EventArgs e) { if (productLstBox.SelectedIndex < 0) { MessageBox.Show("You must choose a product to update."); return; } Electronic electToUpdate = productLstBox.SelectedItem as Electronic; addElectFrm updateForm = new addElectFrm(electToUpdate); updateForm.ShowDialog(); populateList(ElectronicDb.GetAllElectronics()); }
/// <summary> /// When the Delete Electronic button is pressed, a message box is /// presented that prompts the user whether to actually delete the electronic or not. /// If yes is clicked, then the electronic is deleted from the database. Once again, /// the listbox is repopulated if the user clicks yes. Displays an error message if /// there is no electronic selected. /// </summary> private void deleteElectCmd_Click(object sender, EventArgs e) { if (productLstBox.SelectedIndex < 0) { MessageBox.Show("You must select a product to delete."); return; } Electronic elecToDelete = productLstBox.SelectedItem as Electronic; DialogResult result = MessageBox.Show( text: $"Are you sure you want to delete {elecToDelete.ProductId}:{elecToDelete.Manufacturer}:{elecToDelete.Name}", caption: "Delete?", buttons: MessageBoxButtons.YesNo, icon: MessageBoxIcon.Warning); if (result == DialogResult.Yes) { ElectronicDb.Delete(elecToDelete.ProductId); populateList(ElectronicDb.GetAllElectronics()); } }