//Event payment private void btnPayment_Click(object sender,EventArgs e) { using (ConvenienceShopEntities entity = new ConvenienceShopEntities()) { Bill bill = new Bill(); bill.DateOfSale = dtpSaleDate.Value; SaveBill(bill); Bill tmp = new Bill(); tmp = entity.Bills.SqlQuery("Select * from Bill order by Id DESC").FirstOrDefault(); int idx = tmp.Id; int result = 0; for (int i = 0; i < dgvOrderList.Rows.Count - 1; i++) { BillInfo billInfo = new BillInfo(); DataGridViewRow row = dgvOrderList.Rows[i]; billInfo.BillID = idx; billInfo.TypeID = entity.TypeOfProducts.SqlQuery("Select * from TypeOfProduct where TypeName = N'" + row.Cells[0].Value.ToString() + "'").FirstOrDefault().Id; billInfo.ProductID = entity.Products.SqlQuery("Select * from Product where ProductName = N'" + row.Cells[1].Value.ToString() + "'").FirstOrDefault().Id; billInfo.Amount = Convert.ToInt32(row.Cells[3].Value); billInfo.Discount = Convert.ToInt32(row.Cells[5].Value); billInfo.TotalPrice = Convert.ToInt32(row.Cells[6].Value); result = SaveOrder(billInfo); List <Product> t = new List <Product>(); t = entity.Products.SqlQuery("select * from Product where ProductName= N'" + row.Cells[1].Value.ToString() + "' " + " and TypeID=" + billInfo.TypeID + " " + " and Amount > 0").ToList(); foreach (Product ii in t) { if (billInfo.Amount <= ii.Amount) { entity.Database.ExecuteSqlCommand("update Product set Amount=" + (ii.Amount - billInfo.Amount) + " where Id=" + ii.Id); entity.SaveChanges(); break; } entity.Database.ExecuteSqlCommand("update Product set Amount= 0 where Id=" + ii.Id); billInfo.Amount -= ii.Amount; } } if (result == 1) { dgvOrderList.Rows.Clear(); MessageBox.Show("Pay successfully!","Message",MessageBoxButtons.OK,MessageBoxIcon.Information); } else { MessageBox.Show("Can not be paid!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } FormSale_Load(sender,e); } }
//Function save orders onto database public int SaveOrder(BillInfo billInfo) { int result = 0; using (ConvenienceShopEntities entity = new ConvenienceShopEntities()) { entity.BillInfoes.Add(billInfo); entity.SaveChanges(); result = 1; } return(result); }