private void Button_Click_1(object sender, RoutedEventArgs e) { if (Regex.IsMatch(txtno.Text, "^[a-zA-Z]+$") && (Regex.IsMatch(txtCa.Text, "^[a-zA-Z]+$") && (Regex.IsMatch(txtCan.Text, @"^\d+$")))) { bdPrincipal bd = new bdPrincipal(); ProyectoFinal.MiBD.Productos prod = new ProyectoFinal.MiBD.Productos(); prod.NombreProdu = txtno.Text; prod.Categoria = txtCa.Text; prod.Cantidad = int.Parse(txtCan.Text); bd.producto.Add(prod); bd.SaveChanges(); MessageBox.Show("Datos almacenados correctamente"); } else { MessageBox.Show("datos no validos"); } Compras vb = new Compras(); vb.Show(); this.Close(); }
private void Button_Click_1(object sender, RoutedEventArgs e) { ProyectoFinal.MiBD.ProyectoFinal db = new ProyectoFinal.MiBD.ProyectoFinal(); Productos pro = new Productos(); pro.idProducto = int.Parse(pro1.Text); pro.nomProducto = pro2.Text; db.Producto.Add(pro); db.SaveChanges(); MessageBox.Show("Datos correctamente guardados"); }
private void Buscar_Click(object sender, EventArgs e) { //buscar if (Regex.IsMatch(textBox1.Text.Trim(), @"^\d+$")) { bdPrincipal db = new bdPrincipal(); //parse the product code as int from the TextBox int idProd = int.Parse(textBox1.Text); //We query the database for the product MiBD.Productos p = db.producto.SingleOrDefault(x => x.idPro == idProd); if (p != null) //if product was found { //store in a temp variable (if user clicks on add we will need this for the Array) product = p; //We display the product information on a label label2.Text = string.Format("ID: {0}, Name: {1}, Price: {2}", p.idPro, p.NombreProdu, p.Categoria, p.Cantidad); } else { //if product was not found we display a user notification window // MessageBox.Show("Product not found. (Only numbers allowed)", "Product code error", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } }
private void button3_Click(object sender, EventArgs e) { //guardar //we make sure there is at least one item in the cart and a sales person has been selected if (ShoppingCart.Count > 0 && comboBox1.SelectedIndex > -1) { //auto dispose after no longer in scope using (bdPrincipal db = new bdPrincipal()){ //All database transactions are considered 1 unit of work using (var dbTransaction = db.Database.BeginTransaction()) { try { //we create the invoice object invoice inv = new invoice(); inv.SaleDate = DateTime.Now; //assign sales person by querying the database using the Combobox selection //comboBox1.SelectedIndex = 0; inv.id = db.Registro.SingleOrDefault(s => s.id == (int)comboBox1.SelectedValue); //for each product in the shopping cart we query the database foreach (var prod in ShoppingCart) { //get product record with id MiBD.Productos p = db.producto.SingleOrDefault(i => i.idPro == prod.idPro); //reduce inventory int RemainingItems = p.Qty - prod.Qty >= 0 ? (p.Qty - prod.Qty) : p.Qty; if (p.Qty == RemainingItems) { System.Windows.MessageBox.Show( string.Format( "Unable to sell Product #{0} not enough inventory, Do want to continue?", p.idPro), "Not Enough Inventory", MessageBoxButton.OK, MessageBoxImage.Asterisk); //end transaction dbTransaction.Rollback(); //exit procedure return; } else { //If Qty is ok we sell the product p.Qty = RemainingItems; inv.SaleList.Add(p); } } //we add the generated invoice to the Invoice Entity (Table) db.invoices.Add(inv); //Save Changed to the database db.SaveChanges(); // Make the changes permanent dbTransaction.Commit(); //We restore the form with defaults CleanUp(); //Show confirmation message to the user System.Windows.MessageBox.Show(string.Format("Transaction #{0} Saved", inv.invoiceId), "Success", MessageBoxButton.OK, MessageBoxImage.Information); } catch { //if an error is produced, we rollback everything dbTransaction.Rollback(); //We notify the user of the error System.Windows.MessageBox.Show("Transaction Error, unable to generate invoice", "Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } } else { System.Windows.MessageBox.Show("Please select at least one product and a Sales Person", "Data Error", MessageBoxButton.OK, MessageBoxImage.Stop); } }