// G E T P R I C E private double getPrice() { using (var db = new CartEntities()) { int id = int.Parse(cmbProd.SelectedValue.ToString()); var price = (from c in db.Products where c.prod_id == id select c).SingleOrDefault(); return(double.Parse(price.price.ToString())); } }
private void SalesForm_Load(object sender, EventArgs e) { using (var db = new CartEntities()) { cmbProd.DataSource = db.Products.ToList(); cmbProd.DisplayMember = "description"; cmbProd.ValueMember = "prod_id"; cmbProd.SelectedIndex = -1; loaded = true; } }
private void TransactionsForm_Load(object sender, EventArgs e) { using (var db = new CartEntities()) { dgvTrans.DataSource = db.TransDetails.ToList(); dgvTrans.Columns[0].Width = 55; dgvTrans.Columns[1].Width = 55; dgvTransDetails.DataSource = db.Transactions.ToList(); dgvTransDetails.Columns[0].Width = 100; dgvTransDetails.Columns[1].Width = 100; } }
// A D D N E W P R O D U C T private void AddNewProduct() { using (var db = new CartEntities()) { var product = new Product { description = txtDesc.Text, price = double.Parse(txtPrice.Value.ToString()) }; db.Entry(product).State = System.Data.Entity.EntityState.Added; db.SaveChanges(); //MessageBox.Show(product.prod_id + ""); } isNew = false; cmdSave.Enabled = false; txtId.Clear(); txtDesc.Clear(); txtPrice.Value = 0; }
// U P D A T E P R O D U C T private void UpdateProduct() { int id = int.Parse(txtId.Text); Product p = null; using (var db = new CartEntities()) { try { db.Configuration.ProxyCreationEnabled = false; p = db.Products.Where(f => f.prod_id == id).Single(); } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message); return; } } using (var db = new CartEntities()) { bool saveflag = true; do { saveflag = false; try { p.description = txtDesc.Text; p.price = int.Parse(txtPrice.Value.ToString()); db.Entry(p).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); MessageBox.Show("Updated."); isNew = false; cmdSave.Enabled = false; txtId.Clear(); txtDesc.Clear(); txtPrice.Value = 0; } catch (DbUpdateConcurrencyException ex) { saveflag = true; ex.Entries.Single().Reload(); MessageBox.Show("Optimistic concurrency exception occured."); } } while (saveflag); } }
// S E A R C H C L I C K private void cmdSearch_Click(object sender, EventArgs e) { using (var db = new CartEntities()) { var product = db.Products.Find(int.Parse(txtId.Text)); if (product != null) { txtDesc.Text = product.description; txtPrice.Text = product.price.ToString(); isNew = false; cmdSave.Enabled = true; } else { MessageBox.Show("Not found."); isNew = true; cmdSave.Enabled = false; txtId.Clear(); txtDesc.Clear(); txtPrice.Value = 0; } } }
private void cmdPurcahse_Click(object sender, EventArgs e) { int trans_id_inserted; using (var db = new CartEntities()) { using (var dbtrx = db.Database.BeginTransaction()) { try { var trans = new Transaction { total = double.Parse(lbOverallTotal.Text) }; db.Entry(trans).State = System.Data.Entity.EntityState.Added; db.SaveChanges(); trans_id_inserted = trans.trans_id; // last id inserted for (int i = 0; i < lbId.Items.Count; i++) { var trans_details = new TransDetail { prod_id = int.Parse(lbId.Items[i].ToString()), trans_id = trans_id_inserted, price = double.Parse(lbCartPrice.Items[i].ToString()), qty = int.Parse(lbQty.Items[i].ToString()) }; db.Entry(trans_details).State = System.Data.Entity.EntityState.Added; db.SaveChanges(); } dbtrx.Commit(); MessageBox.Show("Successfully recorded transaction"); } catch { MessageBox.Show("Error on recording transaction"); dbtrx.Rollback(); } } } }