Private Sub btnconfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconfirm.Click 'ปุ่มบันทึกข้อมูล
If DataGridView1.Rows.Count > 0 Then
If MessageBox.Show("คุณต้องการยืนยันการขายสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Dim NumRows, i As Integer
Dim comUpdate As New OleDb.OleDbCommand
NumRows = DataGridView1.Rows.Count - 1
For i = 0 To DataGridView1.Rows.Count - 1
sql = "INSERT INTO Sale_Detail (Sale_id, Pro_id,Pro_SalePrice,Pro_amount)"
sql &= " VALUES(" & (DataGridView1.Rows(i).Cells(0).Value) & ","
sql &= (DataGridView1.Rows(i).Cells(1).Value) & ","
sql &= (DataGridView1.Rows(i).Cells(4).Value) & ","
sql &= (DataGridView1.Rows(i).Cells(3).Value) & ")"
cmd = New OleDb.OleDbCommand(sql, accon)
Next
sql = "INSERT INTO Sale (Sale_id, Sale_date )"
sql &= "VALUES (@Sale_id, @Sale_date)"
cmd = New OleDb.OleDbCommand(sql, accon)
Parameter = New OleDb.OleDbParameter
Parameter.ParameterName = "@saledate"
Parameter.OleDbType = OleDb.OleDbType.Date
Parameter.Value = DateTimePicker1.Value
cmd.Parameters.Add(Parameter)
cmd.Parameters.AddWithValue("Sale_id", txt_sale_id.Text)
Try
Catch ErrProcess As Exception '***************************
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ เนื่องจากข้อมูลไม่ถูกต้อง", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
For i = 0 To NumRows
sql = "UPDATE Product SET Pro_stock = Pro_stock - @Pro_stock WHERE(Pro_id = @Pro_id)"
With comUpdate
.Parameters.Clear()
.Parameters.Add("Pro_stock", OleDb.OleDbType.Integer).Value = (DataGridView1.Rows(i).Cells(3).Value)
.Parameters.Add("Pro_id", OleDb.OleDbType.Char).Value = (DataGridView1.Rows(i).Cells(1).Value)
.CommandType = CommandType.Text
.CommandText = sql
.Connection = accon
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกการขายสินค้า เรียบร้อยแล้ว", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
btnstratsale.Enabled = True
ClearAllData()
SaveCancel()
End If
End If
End Sub