'''''''''''''การตัดสต็อกสินค้า'''''''''''''''''''''
Dim QuantityFirst As Integer = 0
Dim QuantityNow As Integer = 0
Dim QuantitySale As Integer = 0
Dim sqlAUp As String = ""
For i = 0 To lv_salepro.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("SELECT Quantity From Product WHERE Product_ID=id")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.ExecuteNonQuery()
End With
Dim dt As DataTable
dt = New DataTable("Quantity")
dr = cmd.ExecuteReader
dt.Load(dr)
QuantityFirst = CInt(CStr((dt.Rows(0)("Quantity"))))
QuantitySale = CInt((lv_salepro.Items(i).SubItems(4).Text))
QuantityNow = QuantityFirst - QuantitySale
sb.Remove(0, sb.Length)
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=id")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("quantity", QuantityNow.ToString)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกข้อมูลการขายสินค้าเรียบร้อยแล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
Private Sub AddSalePro()
If tb_emp.Text.Trim = "" Then
MessageBox.Show("กรุณาระบุรหัสพนักงาน!!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
tb_emp.Focus()
Exit Sub
End If
If tb_mem.Text.Trim = "" Then
MessageBox.Show("กรุณาระบุรหัสมาชิก!!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
tb_mem.Focus()
Exit Sub
End If
If lv_salepro.Items.Count = 0 Then
MessageBox.Show("กรุณาระบุสินค้าที่ขาย!!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
tr = conn.BeginTransaction()
'''''''''''''บันทึกการขายสินค้าของพนักงาน'''''''''''''''''''''
Dim sqladd As String = ""
Dim i As Integer
For i = 0 To lv_salepro.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO SaleProduct (Product_ID,Employee_ID,Sdate,Quantity) ")
sb.Append("VALUES (@proid,@empid,@date,@quantity)")
sqladd = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqladd
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("proid", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("empid", tb_emp.Text.ToString)
.Parameters.AddWithValue("date", CDate(Date_sale.Value.Date))
.Parameters.AddWithValue("quantity", lv_salepro.Items(i).SubItems(4).Text)
.ExecuteNonQuery()
End With
Next
'''''''''''''บันทึกการซื้อสินค้าของสมาชิก'''''''''''''''''''''
sqladd = ""
For i = 0 To lv_salepro.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO BuyProduct (Member_ID,Product_ID,Quantity,PriceUnit,Bdate) ")
sb.Append("VALUES (@memid,@proid,@quantity,@punit,@date)")
sqladd = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqladd
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("memid", tb_mem.Text.ToString)
.Parameters.AddWithValue("proid", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("Ouantity", lv_salepro.Items(i).SubItems(4).Text)
.Parameters.AddWithValue("punit", tb_price.Text)
.Parameters.AddWithValue("date", CDate(Date_sale.Value.Date))
.ExecuteNonQuery()
End With
Next
'''''''''''''การตัดสต็อกสินค้า'''''''''''''''''''''
Dim QuantityFirst As Integer = 0
Dim QuantityNow As Integer = 0
Dim QuantitySale As Integer = 0
Dim sqlAUp As String = ""
For i = 0 To lv_salepro.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("SELECT Quantity From Product WHERE Product_ID=@id")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.ExecuteNonQuery()
End With
Dim dt As DataTable
dt = New DataTable("Quantity")
dr = cmd.ExecuteReader
dt.Load(dr)
QuantityFirst = CInt(CStr((dt.Rows(0)("Quantity"))))
QuantitySale = CInt((lv_salepro.Items(i).SubItems(4).Text))
QuantityNow = QuantityFirst - QuantitySale
sb.Remove(0, sb.Length)
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=@id")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("quantity", QuantityNow)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกข้อมูลการขายสินค้าเรียบร้อยแล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
End Sub
Try
sb.Remove(0, sb.Length)
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=@id")
sqlUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("quantity", QuantityNow)
.ExecuteNonQuery()
End With
Catch ex As OleDbException
MsgBox(ex.Message)
Exit Sub
End Try
'แก้ 2 ที่ครับ
sb.Append("SELECT Quantity From Product WHERE Product_ID=id")
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=id")
'เป็นแบบนี้ แตกต่างกันทีเครื่องหมาย "?" ครับ ลองดูครับ Good Luck
sb.Append("SELECT Quantity From Product WHERE Product_ID=?")
sb.Append("UPDATE Product SET Quantity=? WHERE Product_ID=?")