Code (VB.NET)
[vb]Imports System.Data
Imports System.Data.OleDb
Public Class Form2
Private _dataset As DataSet
Private _conn As OleDbConnection
Private _cmd As OleDbCommand
Private _adapter As OleDbDataAdapter
Private _conStr As String = _
"Provider=microsoft.jet.oledb.4.0;" & _
"data source=D:\Documents\data.mdb"
Private Sub Form2_Load() Handles MyBase.Load
_conn = New OleDbConnection(_conStr)
_conn.Open()
Dim sql As String = "select * from customer"
_cmd = New OleDbCommand(sql, _conn)
Dim reader As OleDbDataReader = _cmd.ExecuteReader()
While reader.Read()
CmbCust.Items.Add(reader("cusname"))
End While
sql = "select * from stock"
_cmd = New OleDbCommand(sql, _conn)
reader = _cmd.ExecuteReader()
While reader.Read
CmbProd.Items.Add(reader("drugname"))
End While
Private Sub showorders()
Dim sql As String
sql = "select * from order where cusname = @cust"
_cmd = New OleDbCommand(sql, _conn)
_cmd.Parameters.AddWithValue("cust", CmbCust.SelectedItem)
_adapter = New OleDbDataAdapter(_cmd)
_dataset = New DataSet()
Private Sub cmbCust_selectedIndexchange() _
Handles CmbCust.SelectedIndexChanged
showorders()
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
Dim sql As String = ""
sql = "INSERT INTO order(cusname,drugname, "
sql &= "Quantity,orderdate)"
sql &= "VALUES(@cust,@prod,@quan,@ordate)"
Dim affedtedRow As Integer = _cmd.ExecuteNonQuery()
If (affedtedRow < 1) Then
ToolStripStatusLabel1.Text = "เกิดการผิดพลาดในการบันทึกข้อมูล"
Return
Else
ToolStripStatusLabel1.Text = "ข้อมูลได้รับการบันทึกแล้ว"
showorders()
End If
End Sub
Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
If (MsgBox("ลบแถวที่เลือก", MsgBoxStyle.OkCancel) = _
MsgBoxResult.Cancel) Then
Return
End If
Dim ordid As String = _
DataGridView1.SelectedRows.Item(0).Cells(0).Value
Dim sql As String
sql = "DELETE * from order where orderid = " & ordid
_cmd.CommandText = sql
_cmd.ExecuteNonQuery()
showorders()
BtnDelete.Enabled = False
End Sub
Private Sub DataGridview1_RowStatechanged() _
Handles DataGridView1.RowStateChanged
If (DataGridView1.SelectedRows.Count > 0) Then
BtnDelete.Enabled = True
Else
BtnDelete.Enabled = False
End If
End Sub
End Class