Private Sub Too_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Too_Save.Click
'---บันทึกตาราง PurchaseOrder
Dim sb As StringBuilder
If TxPOID.Text.Trim() = "" Then
MessageBox.Show("กรุณากดปุ่มเพิ่มใบสั่งซื้อก่อน", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
If MessageBox.Show("คุณต้องการเพิ่มใบสั่งซื้อหรือไม่", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
Dim SqlS As String = ""
sb = New StringBuilder()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO PurchaseOrder(PurchaseOrderID,PurchaseOrderData,ReceiveData,Status,SupplierID)")
sb.Append("VALUES(@poid,@podate,@re,@st,@supid)")
SqlS = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = SqlS
.Connection = cn
cn.Open()
.Parameters.Add("@poid", SqlDbType.NVarChar, 5).Value = TxPOID.Text
.Parameters.Add("@podate", SqlDbType.DateTime).Value = Date_OrderData.Text
.Parameters.Add("@re", SqlDbType.DateTime).Value = Date_ReceiveData.Text
If RD_Pro.Checked = True Then
.Parameters.Add("@st", SqlDbType.NVarChar, 1).Value = "0"
Else
.Parameters.Add("@st", SqlDbType.NVarChar, 1).Value = "1"
End If
.Parameters.AddWithValue("@supid", CStr(CB_Supplier.SelectedValue))
.ExecuteNonQuery()
End With
'---บันทึกตาราง PurchaseOrderDetail
Dim i As Integer = 0
For i = 0 To ListView1.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO PurchaseOrderDetail(PurchaseOrderID,NumberToOrder,Price,Total,ProductID,Netvat,Sumtotal)")
sb.Append("VALUES(@poid,@num,@pri,@tot,@pro,@net,@sum)")
SqlS = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = SqlS
.Parameters.Add("@poid", SqlDbType.NVarChar, 5).Value = TxPOID.Text
.Parameters.AddWithValue("@num", CInt(ListView1.Items(i).SubItems(4).Text))
.Parameters.AddWithValue("@pri", CSng(ListView1.Items(i).SubItems(3).Text))
.Parameters.Add("@tot", SqlDbType.Float).Value = CSng(ListView1.Items(i).SubItems(5).Text)
.Parameters.AddWithValue("@pro", ListView1.Items(i).SubItems(1).Text)
.Parameters.Add("@net", SqlDbType.Float).Value = TxVat.Text
.Parameters.Add("@sum", SqlDbType.Float).Value = TxSUM.Text
.ExecuteNonQuery()
End With
'---บวกเพิ่มสต็อก
sb.Remove(0, sb.Length())
sb.Append("UPDATE Products")
sb.Append("SET UnitsinStock =@un")
sb.Append("WHERE (productid=@proid)")
Dim sqlupate As String
sqlupate = sb.ToString
With cm
.Parameters.Clear()
.CommandText = sqlupate
.Parameters.AddWithValue("@un", CInt(ListView1.Items(i).SubItems(6).Text))
.Parameters.AddWithValue("@proid", ListView1.Items(i).SubItems(1).Text)
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว")
End Sub