Example #1
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();
                    }
                }
            }
        }
Example #2
0
        // 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;
        }
Example #3
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);
            }
        }