01.
Private
Sub
Button4_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
Button4.Click
02.
03.
04.
tran = conn.BeginTransaction()
05.
comedit.Transaction = tran
06.
tran.Begin()
07.
08.
Dim
addpno
As
String
09.
Dim
addqty
As
Integer
10.
Dim
addprice
As
Integer
11.
Dim
checkpiece
As
Integer
12.
Dim
checkprice
As
Integer
13.
Dim
check
As
Integer
14.
check = dgv.Rows.Count - 1
15.
Try
16.
For
d = 0
To
check
17.
addpno = dgv.Rows.Item(d).Cells(0).Value.ToString
18.
addqty = dgv.Rows.Item(d).Cells(2).Value
19.
addprice = dgv.Rows.Item(d).Cells(3).Value
20.
checkpiece = dgv.Rows.Item(d).Cells(4).Value
21.
checkprice = dgv.Rows.Item(d).Cells(5).Value
22.
23.
Dim
cutstock
As
String
24.
cutstock =
"update tbl_Products set ProdQty = '"
& checkpiece - addqty &
"' WHERE (ProdNo='"
& addpno &
"')"
25.
With
comedit
26.
.CommandText = cutstock
27.
.CommandType = CommandType.Text
28.
End
With
29.
30.
31.
Dim
makeorder
As
String
32.
If
RadioButton1.Checked =
True
Then
33.
makeorder =
"insert into tbl_Orders (OrdNo,CustNo,OrdDate,OrdTotal) VALUES (@a,@b,'"
& Format(Now,
"Short Date"
) &
"',@c)"
""
34.
35.
With
comedit
36.
.CommandText = makeorder
37.
.CommandType = CommandType.Text
38.
End
With
39.
comedit.Parameters.AddWithValue(
"a"
, Label4.Text)
40.
comedit.Parameters.AddWithValue(
"b"
, ComboBox1.SelectedValue)
41.
comedit.Parameters.AddWithValue(
"c"
, FormatCurrency(checkprice))
42.
MsgBox(FormatCurrency(checkprice))
43.
Else
44.
makeorder =
"insert into tbl_Orders (OrdNo,CustNo,OrdDate,OrdTotal) VALUES (@a,'c0000','"
& FormatDateTime(Now, DateFormat.ShortDate) &
"',@b)"
45.
46.
With
comedit
47.
.CommandText = makeorder
48.
.CommandType = CommandType.Text
49.
End
With
50.
comedit.Parameters.AddWithValue(
"a"
, Label4.Text)
51.
comedit.Parameters.AddWithValue(
"b"
, FormatCurrency(checkprice))
52.
MsgBox(FormatCurrency(checkprice))
53.
End
If
54.
55.
56.
Dim
orderline
As
String
57.
orderline =
"insert into tbl_Orderlines(OrdNo,ProdNo,Qty,Odl_Price) VALUES (@a,@b,@v,@d)"
58.
59.
With
comedit
60.
.CommandText = orderline
61.
.CommandType = CommandType.Text
62.
End
With
63.
comedit.Parameters.AddWithValue(
"a"
, Label4)
64.
comedit.Parameters.AddWithValue(
"b"
, addpno)
65.
comedit.Parameters.AddWithValue(
"c"
, addqty)
66.
comedit.Parameters.AddWithValue(
"d"
, FormatCurrency(addprice))
67.
68.
Next
69.
MsgBox(addpno)
70.
tran.Commit()
71.
MsgBox(addpno)
72.
Dim
a
As
Integer
73.
a = comedit.ExecuteNonQuery()
74.
MsgBox(
"บันทึกเสร็จสิ้น"
& a, MsgBoxStyle.OkOnly,
"AddOrders"
)
75.
Catch
ex
As
Exception
76.
tran.Rollback()
77.
MsgBox(
"ERROR : "
& ex.Message, MsgBoxStyle.Critical,
"ERROR"
)
78.
End
Try
79.
80.
End
Sub