>i tries to Delete or edit new added roecord it is not working
You need to configure InsertCommand, UpdateCommand, and DeleteCommand properties of dataAdapter.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim Conn As New OleDb.OleDbConnection
Dim Ds As New DataSet
Dim Da As OleDb.OleDbDataAdapter
Dim Cmb As New OleDbCommandBuilder
Dim Dt As New DataTable
Dim Sql As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Conn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0; data source=c:\addressbook.mdb"
Sql = "Select * from tblContacts"
Da = New OleDb.OleDbDataAdapter(Sql, Conn)
Cmb = New OleDbCommandBuilder(Da)
Da.Fill(Ds, "AddressBook")
Dt = Ds.Tables("AddressBook")
DataGridView1.DataSource = Dt
txtFirstName.DataBindings.Add("Text", Dt, "FirstName")
txtSurName.DataBindings.Add("Text", Dt, "Surname")
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Me.BindingContext(Dt).Position += 1
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
Me.BindingContext(Dt).Position -= 1
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
Me.BindingContext(Dt).Position = Me.BindingContext(Dt).Count
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
Me.BindingContext(Dt).Position = 0
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Da.Update(Ds, "AddressBook")
MsgBox("Data Add/edupdated")
DataGridView1.Refresh()
End Sub
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
Me.BindingContext(Dt).AddNew()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
Else
Me.BindingContext(Dt).RemoveAt(Me.BindingContext(Dt).Position)
Da.Update(Ds, "AddressBook")
End If
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dt.DefaultView.RowFilter = "FirstName like '" & txtSearch.Text & "%'"
End Sub
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Dt.DefaultView.RowFilter = "FirstName like '" & txtSearch.Text & "%'"
End Sub
End Class __avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241