Private Sub ButtonSAVE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSAVE.Click
If ListViewOrder.Items.Count > 0 Then
If MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Information
) = Windows.Forms.DialogResult.Yes Then
tr = Conn.BeginTransaction()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Tableorder (Order_ID,Order_Date)")
sb.Append("VALUES (" & TextBoxReceiptid.Text.Trim().Replace("", "") & ",")
sb.Append("" & DateTime.Today & ")")
Dim sqlSave As String = sb.ToString
Dim com = Conn.CreateCommand()
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = Conn
.Transaction = tr
.ExecuteNonQuery()
End With
sb.Remove(0, sb.Length)
sb.Append("SELECT TOP 1 Order_ID FROM TableOrder")
sb.Append("TableOrder BY Order_ID DESC")
sqlSave = sb.ToString()
Dim LastOrder_ID As Integer
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
LastOrder_ID = dr.GetInt32(dr.GetOrdinal("Order_ID"))
Else
LastOrder_ID = 10000
End If
End With
Dim i As Integer
For i = 0 To ListViewOrder.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO [TableOrderDetails] (Order_ID,Product_ID,")
sb.Append("SalePrice,Quantity,TotalSale)")
sb.Append("VALUES (" & LastOrder_ID & ",")
sb.Append("" & ListViewOrder.Items(i).SubItems(0).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(1).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(4).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(5).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(6).Text & ",0)")
sqlSave = sb.ToString()
With com
.CommandText = sqlSave
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกรายการสั่งซื้อสินค้าเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
ListViewOrder.Clear()
ClearProductData()
textboxNetTotal.Text = "0"
End If
End If
End Sub
End Class
Inherits System.Windows.Forms.Form
Const database_file As String = ".\DBMarketProgram.mdb"
Dim strconnection As String = "Provider = Microsoft.jet.OLEDB.4.0;" & "Data Source =" & database_file
Dim cn As New OleDb.OleDbConnection(strconnection)
Dim da As New OleDb.OleDbDataAdapter
Dim Conn As DbConnection
Dim dr As DbDataReader
Dim tr As DbTransaction
Inherits System.Windows.Forms.Form
Const database_file As String = ".\DatabaseProgramMarKet.mdb"
Dim strconnection As String = "Provider = Microsoft.jet.OLEDB.4.0;" & "Data Source =" & database_file
Dim cn As New OleDb.OleDbConnection(strconnection)
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim dr As DbDataReader
Dim tr As DbTransaction
Dim dc As New OleDbCommand
Dim strConn As String = ""
Dim sb As New StringBuilder()
Dim IsFind As Boolean = False
Dim IsFindProduct As Boolean = False
Dim WithEvents objCurrencymanager As CurrencyManager
ส่วนนี้ คือ code บันทึก Code (VB.NET)
If ListViewOrder.Items.Count > 0 Then
End If
If MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Information
) = Windows.Forms.DialogResult.Yes Then
tr = cn.BeginTransaction()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Tableorder (Order_ID,Order_Date)")
sb.Append("VALUES (" & TextBoxReceiptid.Text.Trim().Replace("", "") & ",")
sb.Append("" & DateTime.Today & ")")
Dim sqlSave As String = sb.ToString
Dim com = cn.CreateCommand()
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = cn
.Transaction = CType(tr, OleDbTransaction)
.ExecuteNonQuery()
End With
tr.Commit()
sb.Remove(0, sb.Length)
sb.Append("SELECT TOP 1 Order_ID FROM TableOrder")
sb.Append("TableOrder BY Order_ID DESC")
sqlSave = sb.ToString()
Dim LastOrder_ID As Integer
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = cn
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
LastOrder_ID = dr.GetInt32(dr.GetOrdinal("Order_ID"))
Else
LastOrder_ID = 10003
End If
End With
dr.Close()
Dim i As Integer
For i = 0 To ListViewOrder.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO [TableOrderDetails] (Order_ID,Product_ID,")
sb.Append("SalePrice,Quantity,TotalSale)")
sb.Append("VALUES (" & LastOrder_ID & ",")
sb.Append("" & ListViewOrder.Items(i).SubItems(0).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(1).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(4).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(5).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(6).Text & ",0)")
sqlSave = sb.ToString()
With com
.CommandText = sqlSave
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกรายการสั่งซื้อสินค้าเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
ListViewOrder.Clear()
ClearProductData()
textboxNetTotal.Text = "0"
cn.Close()
End If