Imports System.Data
Imports System.Data.OleDb
Public Class frmMemberchk
Const database_file As String = "C:\Users\Patongko01\Documents\Visual Studio 2010\Projects\OVOCAFE\OVOCAFE\ovocafe.mdb"
Dim strConnection As String = "provider=microsoft.Jet.Oledb.4.0;" & "Data Source=" & database_file
Dim conn As New OleDbConnection(strConnection)
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Private Sub frmMemberchk_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'OvocafeDataSet.member' table. You can move, or remove it, as needed.
Me.MemberTableAdapter.Fill(Me.OvocafeDataSet.member)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
End Sub
Private Sub btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
If MessageBox.Show("Are you sure to delete?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Dim strID As String = Me.DataGridView1(0, Me.DataGridView1.CurrentCell.RowIndex).Value.ToString()
conn.Open()
Dim cmd As OleDbCommand = conn.CreateCommand()
cmd.CommandText = "DELETE FROM [member] WHERE id = '" & strID & "'"
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
conn.Close()
MessageBox.Show("Delete Successfully")
End If
End Sub
Private Function eRowIndex() As Object
Throw New NotImplementedException
End Function
End Class
Private Sub btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
If MessageBox.Show("Are you sure to delete?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Dim strID As Integer = Me.DataGridView1(0, Me.DataGridView1.CurrentCell.RowIndex).Value.ToString()
conn.Open()
Dim cmd As OleDbCommand = conn.CreateCommand()
cmd.CommandText = "DELETE FROM [member] WHERE IDMember = " & strID & ""
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
conn.Close()
MessageBox.Show("Delete Successfully")
End If
End Su
ส่วนใน Access แก้ IDMember จาก text เป็น Number ครับ
แก้จาก strlD จาก String เป็น Integer
แก้ส่วนของ DELETE FROM ตามท่าน SeedNew ครับผม
แก้ได้แล้วครับ
ขอสอบถามเพิ่มอีกนิดครับ ถ้าอยากลบแล้วให้ตารางอัพเดทเลย ต้องใช้คำสั่งอะไรครับผม
พอดีลบแล้วตารางมันไม่อัพเดทครับ ต้องปิดหน้า form แล้วเปิดใหม่ถึงจะอัพเดทครับ
โค๊ดตามด้านล่างนี้เลยครับ
Code (VB.NET)
Imports System.Data
Imports System.Data.OleDb
Public Class frmMemberchk
Const database_file As String = "C:\Users\Patongko01\Documents\Visual Studio 2010\Projects\OVOCAFE\OVOCAFE\ovocafe.mdb"
Dim strConnection As String = "provider=microsoft.Jet.Oledb.4.0;" & "Data Source=" & database_file
Dim conn As New OleDbConnection(strConnection)
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Private Sub frmMemberchk_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'OvocafeDataSet.member' table. You can move, or remove it, as needed.
Me.MemberTableAdapter.Fill(Me.OvocafeDataSet.member)
End Sub
Private Sub BindDataGrid()
Dim dt As New DataTable
conn.Open()
Dim myCommand As OleDbCommand = conn.CreateCommand()
myCommand.CommandText = "SELECT [IDMember], [MemberCount], [MemberTotal] FROM [member]"
myCommand.CommandType = CommandType.Text
da = New OleDbDataAdapter(myCommand)
da.Fill(dt)
conn.Close()
Me.DataGridView1.DataSource = dt
Me.DataGridView1.Columns.Clear()
Dim column As DataGridViewTextBoxColumn
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "IDMember"
Me.DataGridView1.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "MemberCount"
Me.DataGridView1.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "MemberTotal"
Me.DataGridView1.Columns.Add(column)
dt = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
End Sub
Private Sub btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
If MessageBox.Show("Are you sure to delete?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Dim strID As Integer = Me.DataGridView1(0, Me.DataGridView1.CurrentCell.RowIndex).Value.ToString()
conn.Open()
Dim cmd As OleDbCommand = conn.CreateCommand()
cmd.CommandText = "DELETE FROM [member] WHERE IDMember = " & strID & ""
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
conn.Close()
MessageBox.Show("Delete Successfully")
BindDataGrid()
End If
End Sub
Private Function eRowIndex() As Object
Throw New NotImplementedException
End Function
End Class