Imports System.Data
Imports System.Data.OleDb
Public Class frm_order
Dim DR As OleDbDataReader
Dim DT As DataTable
Dim DC As New OleDbCommand
Dim DC1 As New OleDbCommand
Dim DA As OleDbDataAdapter
Dim DA1 As OleDbDataAdapter
Dim DS As New DataSet
Dim DS1 As New DataSet
Dim TR As OleDbTransaction
Dim TR1 As OleDbTransaction
Dim SAVE As String = ""
Private Sub frm_order_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ConnectDB()
ShowData()
Formatdatagrid()
EnableAll()
autoid()
txt_sum.Text = "0"
End Sub
Sub Reset()
txt_productid.Clear()
txt_product.Clear()
txt_price.Clear()
txt_qty.Clear()
txt_cusid.Clear()
txt_username.Clear()
GV_ADD.Rows.Clear()
txt_sum.Text = "0"
End Sub
Sub EnableAll()
add.Enabled = False
save1.Enabled = False
End Sub
Sub ShowData()
Dim sqlshow As String
sqlshow = "SELECT * FROM product Order by pro_id ASC "
DS.Clear()
DA = New OleDbDataAdapter(sqlshow, CONN)
DA.Fill(DS, "Product")
GV.DataSource = DS.Tables("Product")
End Sub
Sub Formatdatagrid()
With GV
.ReadOnly = True 'สังให้ DataGrid สามารถอ่านได้อย่างเดียว
If .RowCount > 0 Then
.Font = New Font("Tahoma", 9, FontStyle.Bold)
.Columns(0).HeaderText = "รหัสสินค้า"
.Columns(1).HeaderText = "ชื่อสินค้า"
.Columns(2).HeaderText = "ราคา"
.Columns(0).Width = 100
.Columns(1).Width = 100
.Columns(2).Width = 80
.AlternatingRowsDefaultCellStyle.BackColor = Color.White
.DefaultCellStyle.BackColor = Color.AntiqueWhite
End If
End With
End Sub
Sub autoid()
Dim tmpautoid As Integer = 0
Dim sqlauto As String
sqlauto = "SELECT order_id From tb_order Order By order_id DESC"
DC.CommandType = CommandType.Text
DC.CommandText = sqlauto
DC.Connection = CONN
DA = New OleDbDataAdapter(DC)
DS1.Clear()
DA.Fill(DS1, "number")
tmpautoid = (DS1.Tables("number").Rows.Count) + 1
txt_order.Text = Format("PO" + tmpautoid.ToString("000000000"))
End Sub
Sub Add_Data2Grid()
GV_ADD.AllowUserToAddRows = True
GV_ADD.Rows.Add()
GV_ADD.ClearSelection()
With GV_ADD
Dim r As Integer = .Rows.Count - 2
.Rows(r).Cells(0).Value = txt_order.Text.Trim
.Rows(r).Cells(1).Value = txt_productid.Text.Trim
.Rows(r).Cells(2).Value = txt_product.Text.Trim
.Rows(r).Cells(3).Value = txt_price.Text.Trim
.Rows(r).Cells(4).Value = txt_qty.Text.Trim
.Rows(r).ReadOnly = True
.CurrentCell = .Rows(r).Cells(1)
End With
GV_ADD.AllowUserToAddRows = False
txt_productid.Text = String.Empty
txt_product.Text = String.Empty
txt_price.Text = String.Empty
txt_qty.Text = String.Empty
txt_qty.Focus()
End Sub
Private Sub GV_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GV.CellContentClick
Dim userselectrow As Integer = 0
userselectrow = GV.CurrentRow.Index
With GV
txt_productid.Text = (GV.Item(0, userselectrow)).Value.ToString
txt_product.Text = (GV.Item(1, userselectrow)).Value.ToString
txt_price.Text = (GV.Item(2, userselectrow)).Value.ToString
End With
add.Enabled = True
save1.Enabled = True
End Sub
Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click
SAVE = "ADD"
If txt_order.Text = String.Empty And txt_productid.Text = String.Empty Or txt_product.Text = String.Empty Or txt_price.Text = String.Empty Or txt_qty.Text = String.Empty Then
MessageBox.Show("กรุณาเลือกข้อมูลการสั่งซื้อ")
ElseIf txt_qty.Text = String.Empty Then
MessageBox.Show("กรุณาใส่จำนวน")
txt_qty.Focus()
Else
Add_Data2Grid()
End If
Dim i As Integer
Dim tmp As Integer = 0
Dim total As Integer = 0
Dim count As Integer = GV_ADD.Rows.Count - 1
For i = 0 To count
tmp = (CInt(GV_ADD.Rows(i).Cells(3).Value)) * (CInt(GV_ADD.Rows(i).Cells(4).Value))
total += tmp
txt_sum.Text = CStr(total)
Next
End Sub
Private Sub save1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save1.Click
TR = CONN.BeginTransaction()
If txt_order.Text = "" Then
MessageBox.Show("กรุณากรอกรหัสใบสั่งซื้อ")
txt_order.Focus()
TR.Rollback()
Exit Sub
Else
Dim sqlsave As String = String.Empty
Dim sqlsaveline As String = String.Empty
Dim tmpmsg As String
Try
Select Case SAVE
Case "ADD"
'Header Item ส่วนการเพิ่มข้อมูล ตาราง tb_order
sqlsave = "INSERT INTO tb_order VALUES('" & txt_order.Text & "'" & _
",'" & Now() & "','" & txt_sum.Text & "','A','" & txt_cusid.Text & "')"
End Select
If SAVE = "ADD" Then
tmpmsg = "คุณต้องการเพิ่มข้อมูลใช่หรือไม่"
Else
tmpmsg = "คุณต้องการแก้ไขข้อมูลใช่หรือไม่"
End If
If MessageBox.Show(tmpmsg, "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
DC.CommandText = sqlsave
DC.CommandType = CommandType.Text
DC.Connection = CONN
DC.Transaction = TR
DA = New OleDbDataAdapter(DC)
DA.Fill(DS, "Order")
TR.Commit()
DS.Clear()
MessageBox.Show("เพิ่มใบสั่งซืื้อเรียบร้อย")
Else
TR.Rollback()
End If
Catch ex As Exception
MessageBox.Show("เกิดข้อผิดพลาด", "ตรวจสอบข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
TR.Rollback()
End Try
'ตรงช่วงนี้ครับ ----------> Line Item ส่วนการเพิ่มข้อมูล ตาราง orderdetail
For i = 0 To GV_ADD.Rows.Count - 1
DC1.Parameters.Add("order", OleDb.OleDbType.VarChar).Value = GV_ADD.Rows(i).Cells(0).Value
DC1.Parameters.Add("proid", OleDb.OleDbType.VarChar).Value = GV_ADD.Rows(i).Cells(1).Value
DC1.Parameters.Add("price", OleDb.OleDbType.VarChar).Value = GV_ADD.Rows(i).Cells(4).Value
sqlsaveline = "insert into orderdetail(order_id,pro_id,order_qty)values(order,proid,price)"
DC1.CommandText = sqlsaveline
DC1.CommandType = CommandType.Text
DC1.Connection = CONN
DC1.ExecuteNonQuery()
Next
ShowData()
Formatdatagrid()
EnableAll()
autoid()
Reset()
End If
End Sub
Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
Reset()
End Sub
Private Sub back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles back.Click
Me.Close()
End Sub
End Class