private void buttonComplete_Click(object sender, EventArgs e) { if (MessageBox.Show("Xác Nhận Nhập Hàng ^_^ ", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes) { //Thêm dữ liệu vào bảng hóa đơn nhập try { model = GlobalConfig.Connection.InsertNewEntryInvoice(Employee.ID, Supplier.ID, DateEntryPicker.Value.ToString("yyyy-MM-dd HH:mm:ss"), tongtien); } catch (Exception r) { MessageBox.Show($"{r.ToString()} Lỗi câu lệnh sql ~ 1 ~ line 186", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } //Thêm dữ liệu vào bảng chi tiết hóa đơn nhập foreach (DataGridViewRow row in dataGridView1.Rows) { if (row != null) { try { decimal unitprice = decimal.Parse(row.Cells["UnitPrice"].Value.ToString()); decimal quantity = decimal.Parse(row.Cells["Quantity"].Value.ToString()); decimal discount = (1 - decimal.Parse(row.Cells["Discount"].Value.ToString()) / 100); //Truy Vấn SQL GlobalConfig.Connection.InsertNewEntryDetails( model.ID, row.Cells["ProductID"].Value.ToString(), float.Parse(row.Cells["Discount"].Value.ToString()), decimal.Parse(row.Cells["UnitPrice"].Value.ToString()), int.Parse(row.Cells["Quantity"].Value.ToString()), (unitprice * quantity) * discount ); // dataGridView1.Rows.Clear(); // labelTotal.Text = "Tổng Tiền : 0 VND"; } catch { MessageBox.Show(" Lỗi câu lệnh sql ~ 2 ~ line 199", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } MessageBox.Show("Thêm hóa đơn thành công!", "Thông Báo", MessageBoxButtons.OK); this.Dispose(); } }
//---------------------------------------------------------------------------------------------------------------------------------- #region Các Câu lệnh Insert đối tượng chính //Tạo Hóa đơn nhập mới public Model_EntryInvoice InsertNewEntryInvoice(string emID, string supID, string day, decimal total) { using (IDbConnection connection = new SqlConnection(GlobalConfig.ConnectionString("Clothes"))) { Model_EntryInvoice model = new Model_EntryInvoice(); var p = new DynamicParameters(); p.Add("@EmployeeID ", emID); p.Add("@SupplierID", supID); p.Add("@DayEntry", day); p.Add("@Total", total); p.Add("@EntryID", "", DbType.String, direction: ParameterDirection.Output); connection.Execute("dbo.InsertNewEntryInvoice", p, commandType: CommandType.StoredProcedure); model.ID = p.Get <string>("@EntryID"); model.SupplierID = supID; model.Day = DateTime.Parse(day); model.EmployeeID = emID; model.Total = total; return(model); } }