private void DeleteList_Button(object sender, RoutedEventArgs e) { if (ProductsListView.SelectedItems.Count == 1) { Lists l = ProductsListView.SelectedItem as Lists; db.Lists.Remove(l); db.SaveChanges(); } }
private void AddList_Button(object sender, RoutedEventArgs e) { list.Name = lNameTextBox.Text; foreach (ProductsLists pl in prodList) { list.ProductsLists.Add(pl); } db.Lists.Add(list); db.SaveChanges(); Close(); }
private void Delete_Button(object sender, RoutedEventArgs e) { if (ProductsLV.SelectedItems.Count >= 1) { List <Products> l = ProductsLV.SelectedItems.OfType <Products>().ToList(); foreach (Products product in l) { db.Products.Remove(product); } db.SaveChanges(); } }
private void AddButton_Click(object sender, RoutedEventArgs e) { Regex nameRegex = new Regex(namePattern), numberRegex = new Regex(numberPattern); if (nameRegex.IsMatch(pNameTextBox.Text) && numberRegex.IsMatch(pWeightTextBox.Text) && numberRegex.IsMatch(pCaloriesTextBox.Text)) { using (CaloriesEntities db = new CaloriesEntities()) { if (_product.Id != 0) { var result = db.Products.SingleOrDefault(b => b.Id == _product.Id); if (result != null) { result.Name = pNameTextBox.Text; try { result.weight = Int32.Parse(pWeightTextBox.Text); result.calories = Int32.Parse(pCaloriesTextBox.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } else { _product.Name = pNameTextBox.Text; try { _product.weight = Int32.Parse(pWeightTextBox.Text); _product.calories = Int32.Parse(pCaloriesTextBox.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } db.Products.Add(_product); try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } this.DialogResult = true; } else { SystemSounds.Hand.Play(); } }