Imports System.Data.OleDb
Public Class ProductType
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Project\core_song.accdb")
Dim ObjCurrencyMgr As CurrencyManager
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
Dim dr As OleDbDataReader
Dim dt As DataTable
Dim command As OleDbCommand
Dim task As String = ""
Private Sub showData()
conn.Open()
Dim ds As New DataSet
Dim sqlCmd As String = "SELECT * From ProductType ORDER BY รหัสประเภทสินค้า DESC"
da = New OleDbDataAdapter(sqlCmd, conn)
da.Fill(ds, "ProductType")
DGV1.DataSource = ds.Tables(0)
DGV1.Columns(0).HeaderText = "รหัสประเภทสินค้า"
DGV1.Columns(1).HeaderText = "ชื่อประเภทสินค้า"
DGV1.Columns(0).Width = 100
DGV1.Columns(1).Width = 170
conn.Close()
Binding()
setDisable()
End Sub
Private Sub Binding()
Dim ObjC As CurrencyManager
ObjC = Me.BindingContext(DGV1.DataSource, "")
txtID.DataBindings.Clear()
txtName.DataBindings.Clear()
txtID.DataBindings.Add("text", DGV1.DataSource, "รหัสประเภทสินค้า")
txtName.DataBindings.Add("text", DGV1.DataSource, "ชื่อประเภทสินค้า")
End Sub
Private Sub maxId()
Dim maxId As Integer = 0
conn.Open()
Dim sqlCmd As String = "Select Max(รหัสประเภทสินค้า) From ProductType"
command = New OleDbCommand(sqlCmd, conn)
Try
maxId = command.ExecuteScalar()
Catch ex As Exception
maxId = 0
End Try
txtID.Text = maxId + 1
conn.Close()
End Sub
Private Sub setDisable()
txtName.Enabled = False
End Sub
Private Sub setEnable()
txtName.Enabled = True
End Sub
Private Sub setBtnDisable()
btnAdd.Enabled = False
btnEdit.Enabled = False
btnDelete.Enabled = False
btnSave.Enabled = True
btnCancel.Enabled = True
End Sub
Private Sub setBtnEnable()
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDelete.Enabled = True
btnSave.Enabled = False
btnCancel.Enabled = False
End Sub
Private Sub clearText()
txtID.Text = ""
txtName.Text = ""
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
task = "Add"
clearText()
setEnable()
maxId()
setBtnDisable()
End Sub
Private Sub product_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
showData()
setBtnEnable()
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
task = "Edit"
setEnable()
setBtnDisable()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlCmd As String = ""
If (task.Equals("Add")) Then
sqlCmd = "Insert Into ProductType(รหัสประเภทสินค้า,ชื่อประเภทสินค้า) Values(" & txtID.Text & ", '" & txtName.Text & "')"
ElseIf (task.Equals("Edit")) Then
sqlCmd = "Update ProductType SET "
sqlCmd &= "ชื่อประเภทสินค้า = '" & txtName.Text & "' Where รหัสประเภทสินค้า = " & txtID.Text
End If
MessageBox.Show(sqlCmd)
'dbQuery(sqlCmd) **************************************************************ตัวนี้คืออะไรครับ มันติดตรงนี้ครับหรือป่าวครับ
showData()
setBtnEnable()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
clearText()
setDisable()
showData()
setBtnEnable()
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("คุณต้องการลบข้อมูลนี้", "คำเตือน", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then Dim sqlCmd As String = "Delete From ProductType Where รหัสประเภทสินค้า = " & txtID.Text
'dbQuery(sqlCmd)**************************************************************ตัวนี้คืออะไรครับ มันติดตรงนี้ครับหรือป่าวครับ
End If
showData()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
dbQuery(...) ผมว่าเป็น Function หนึ่งที่ประกาศไว้แล้วไปคุณ copy มาไม่ครบ
TxtID.Text คุณไม่มีค่า Null หรือเป็น string ว่าง ๆ "" ทำให้ error Index or primary key cannot contain a Null value
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
conn.Open()
If MessageBox.Show("คุณต้องการที่จะลบข้อมูลนี้", "คำเตือน", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
Dim sqlCmd As String = "DELETE FROM Product WHERE รหัสสินค้า = " & txtID.Text
'dbQuery(sqlCmd)
command = New OleDbCommand(sqlCmd, conn)
command.ExecuteNonQuery()
End If
conn.Close()
End Sub