I'm having trouble trying to update or delete records from my database.
Basically, I have a datagridview(DGV) in my form and the following is what I wanna do.
The user could directly change/edit the values in the DGV and just click the update button to update the database. And the user could select the row that he/she wish to delete. Once the row is selected, the delete button will be clicked. The row will be deleted from DGV and the database too.
So this is my code so far. Everything is working as expected, excluding the Update button and Delete button. Please help me out. I have no idea how to delete at all.
Imports System.Data
Imports System.Data.OleDb
Public Class AdminConsole
'Global controls
Dim Conn As New OleDbConnection
Dim Cmd As New OleDbCommand()
Dim da As New OleDbDataAdapter(Cmd)
Dim dt As New DataTable
Dim ds As New DataSet
Private Sub AdminConsole_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Conn.Close()
End Sub
Private Sub AdminConsole_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Cmd = New OleDbCommand("SELECT * FROM staffs", New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source =KDE.mdb;"))
Me.da = New OleDbDataAdapter(Me.Cmd)
Me.ds = New DataSet()
Me.Cmd.Connection.Open()
Me.da.Fill(Me.ds)
Me.Cmd.Connection.Close()
Me.dataGridAdmin.DataSource = Me.ds.Tables(0).DefaultView
End Sub
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
NewStaff.Show()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Me.Cmd = New OleDbCommand("UPDATE * FROM staffs", New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source =KDE.mdb;"))
Me.da = New OleDbDataAdapter(Me.Cmd)
Me.ds = New DataSet()
Me.Cmd.Connection.Open()
Me.da.Update(ds)
Me.Cmd.Connection.Close()
Me.dataGridAdmin.DataSource = Me.ds.Tables(0).DefaultView
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
Me.Cmd = New OleDbCommand("SELECT * FROM staffs", New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source =KDE.mdb;"))
Me.da = New OleDbDataAdapter(Me.Cmd)
Me.ds = New DataSet()
Me.Cmd.Connection.Open()
Me.da.Fill(Me.ds)
Me.Cmd.Connection.Close()
Me.dataGridAdmin.DataSource = Me.ds.Tables(0).DefaultView
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
'Delete command goes here.
End Sub
End Class