Imports System.Data
Imports System.Data.OleDb
Imports System.Text
Public Class Product
Dim Conn As New OleDbConnection
Dim Com As OleDbCommand
Dim dr As OleDbDataReader
Dim sb As New StringBuilder
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim IsFind As Boolean
Dim dtCategory As DataTable
Private Sub Product_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
ShowCategory()
End Sub
'ซับรูทีนสำหรับดึงข้อมูลตารางประเภทสินค้ามาแสดงในคอนโทรล ComboBox
Sub ShowCategory()
Dim sql As String
sb = New StringBuilder()
sb.Append("SELECT CategoryID,CategoryName FROM Category;")
sql = sb.ToString()
Com = New OleDbCommand
With Com
.CommandText = sql
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
End With
If dr.HasRows Then
dtCategory = New DataTable() 'สร้างตรารางขึ้นมาอ่าน
dtCategory.Load(dr)
With cboCategory 'ชื่อ combonbox ที่จะโชว์
.BeginUpdate()
.DisplayMember = "CategoryName" 'โชว์ชื่อ ฟิลต์ชื่อประเภทสินค้า
.ValueMember = "CategoryID" 'ผูกติดกับPrimaryKey ฟิล์ด รหัสประเภทสินค้า
.DataSource = dtCategory
.EndUpdate()
End With
End If
dr.Close()
End Sub
Private Sub GenerateID()
Dim sql As String = ""
Dim tmpID As String = ""
Dim LastID As String = ""
Dim tmpTransID As Integer = 0
sql = "SELECT TOP 1 ProductID FROM Product ORDER BY ProductID DESC"
Try
Com = New OleDbCommand
With Com
.CommandText = sql
.Commandtype = Commandtype.text
.Connection = Conn
dr = .ExecuteReader
dr.Read()
tmpTransID = CInt(dr.Item("ProductID"))
tmpTransID = tmpTransID + 1
LastID = tmpTransID.ToString("00000")
txtProductID.Text = LastID
End With
Catch ex As Exception
LastID = "00001"
txtProductID.Text = LastID
End Try
dr.Close()
End Sub
Sub ClearAllData()
txtProductID.Clear()
txtProductName.Clear()
mskSerialNo.Clear()
dtpProductDate.Value = Today
txtBrand.Clear()
txtUnitPerCost.Clear()
txtUnitPerPrice.Clear()
txtUnitInStock.Clear()
txtUnitReOrder.Clear()
End Sub
Private Sub tsClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If MessageBox.Show("คุณต้องการออกจากระบบใช้หรือไม่???", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub
Private Sub tsClear_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsClear.Click
ClearAllData()
End Sub
Private Sub tsBrowseProduct_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsBrowseProduct.Click
Dim fAllProduct As New AllProduct
fAllProduct.ShowDialog(Me)
txtProductID.Text = rProductId
txtProductName.Text = rProductName
mskSerialNo.Text = rSerialNo
txtBrand.Text = rBrand
txtUnitPerCost.Text = CDec(rUnitperCost)
txtUnitPerPrice.Text = CDec(rUnitperPrice)
txtUnitInStock.Text = CInt(rUnitInStock)
txtUnitReOrder.Text = CInt(rUnitReOrder)
rProductId = ""
rProductName = ""
rSerialNo = ""
rBrand = ""
rUnitperCost = 0.0
rUnitperPrice = 0.0
rUnitInStock = 0
rUnitReOrder = 0
End Sub
Private Sub tsSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsSave.Click
If MessageBox.Show("คุณต้องการบันทึกข้อมูลใช่หรือไม่???", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Try
Dim sql As String
sb.Remove(0, sb.Length())
sb.Append("INSERT INTO Product(ProductID,ProductName,SerialNo,ProductDate,Brand,CategoryName,UnitPerCost,UnitPerPrice,UnitInStock,UnitReOrDer,CategoryID)")
sb.Append(" VALUES(@ProductID,@ProductName,@SerialNo,@ProductDate,@Brand,@CategoryName,@UnitPerCost,@UnitPerPrice,@UnitInStock,@UnitReOrDer,@CategoryID)")
sql = sb.ToString
Com = New OleDbCommand
With Com
.CommandText = sql
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
' เป็น Text ใช้ varchar ,เป็น วันที ใช้ deta จะต้องแมทกับ Access ด้วย
.Parameters.Add("@ProductID", OleDbType.VarChar).Value = txtProductID.Text.Trim()
.Parameters.Add("@ProductName", OleDbType.VarChar).Value = txtProductName.Text.Trim()
.Parameters.Add("@SerialNo", OleDbType.VarChar).Value = mskSerialNo.Text.Trim()
.Parameters.Add("@ProductDate", OleDbType.Date).Value = dtpProductDate.Value
.Parameters.Add("@Brand", OleDbType.VarChar).Value = txtBrand.Text.Trim() 'SelectedItem.ToString เป็นการแอดค่าที่เราเพิ่มไป
.Parameters.Add("@CategoryName", OleDbType.VarChar).Value = cboCategory.SelectedText
.Parameters.Add("@UnitPerCost", OleDbType.Double).Value = txtUnitPerCost.Text.Trim()
.Parameters.Add("@UnitPerPrice", OleDbType.Double).Value = txtUnitPerPrice.Text.Trim()
.Parameters.Add("@UnitInStock", OleDbType.Integer).Value = txtUnitInStock.Text.Trim()
.Parameters.Add("@UnitReOrDer", OleDbType.Integer).Value = txtUnitReOrder.Text.Trim()
'If radSale.Checked = True Then
' .Parameters.Add("@ProductStatus", OleDbType.VarChar).Value = radSale.Text '"จำหน่าย"
'Else
' .Parameters.Add("@ProductStatus", OleDbType.VarChar).Value = radCancelSale.Text '"ยกเลิกการจำหน่าย"
'End If
.Parameters.Add("@CategoryID", OleDbType.VarChar).Value = cboCategory.SelectedValue 'SelectedValue เป็นการดึงการฐานข้อมูล
.ExecuteNonQuery()
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("ยกเลิกการบันทึกข้อมูล", "ผลกการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub tsAdd_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsAdd.Click
GenerateID()
End Sub
End Class