private void SaveButton_Click(object sender, EventArgs e) { Double amount = 0; int qty = 0, id; string name = NameTextBox.Text; Int32.TryParse(IdTextBox.Text, out id); if (!(name == "")) { try { amount = Convert.ToDouble(AmountTextBox.Text); qty = Convert.ToInt32(QuantityTextBox.Text); Product product = new Product(name, amount, qty); manager.addProduct(product); cleanComponents(); ErrorLabel.Text = "Registro agregado satisfactoriamente."; ErrorLabel.Visible = true; } catch (Exception ex) { ErrorLabel.Text = "Favor llene los campos debidamente"; ErrorLabel.Visible = true; } } else { ErrorLabel.Text = "Favor llene los campos debidamente"; ErrorLabel.Visible = true; } }
public void addProduct(Product product) { bool exists = false; Product mProduct = null; List<Product> Products = new List<Product>(); { Products = Deserialize(ProductFilePath) as List<Product>; if (Products != null) { if (product.Id != 0 && (product.Id%1000)>0) { try { mProduct = Products.ElementAt(product.Id % 1000); } catch (IndexOutOfRangeException e) { } catch (NullReferenceException e) { } } else { foreach (Product p in Products) { if (product.Name == p.Name) { mProduct = p; } } } } else { Products = new List<Product>(); } if (mProduct != null) { exists = true; Products.Remove(mProduct); mProduct.Amount = product.Amount; if (product.Quantity > mProduct.Quantity) { mProduct.Quantity = product.Quantity; } Products.Add(mProduct); } } if (!exists) { ProductId = ProductId + Products.Count() + 1; product.Id = ProductId; Products.Add(product); } Stream write = File.Open(ProductFilePath, FileMode.Create); Serializer.Serialize(write, Products); write.Close(); }