digitalsindhx 0 Newbie Poster

Help With Add, Delete, Edit and Update Record (with Datatable Method)

I Created a Software with OleDb .mdb database previously Using Dataset Method, but I was unable to Connect Datagrid with it on real Basis, I mean when I Clicked Next Record Button it only Moves in TextBoxes (Which were Connected with Dataset) but on DataGrid it was not moving the Curser to next record or previous Record,

Now with the Help and Advice of <Adatapost> (A member of this Forum) I Resolved it with using DataTable function instead of dataset

Now Plz I need a little more Help here, how to Add, Delete, Edit and update Record with DataTable Method

The Code now I m using is

‘Name space Declearations
Imports System.Data
Imports System.Data.OleDb

‘Declearations in form1 class for dataTable, DataAdapter, Maximum Rows of DataTable, 

Public Class Form1

Dim dtBook As DataTable
Dim daBook As OleDb.OleDbDataAdapter



‘I Created a function for Clearing and then adding Databindings with TextBoxes for multiple sql Queries time to time
Public Function ClearBookTable()

        txtBookID.DataBindings.Clear()
        txtNameOfBook.DataBindings.Clear()
        txtCompanyID.DataBindings.Clear()

txtBookID.DataBindings.Add("text", dtBook, "BookID")
        txtNameOfBook.DataBindings.Add("text", dtBook, "NameOfBook")
        txtCompanyID.DataBindings.Add("text", dtBook, "CompanyID")
        txtAuthorName.DataBindings.Add("text", dtBook, "AuthorName")


end function


‘On load Event of form I added code to Connect and retrive data from mdb file
Private Sub Form1_Load()

Dim daBook As OleDb.OleDbDataAdapter



        dtBook = New DataTable(1)
        daBook = New OleDbDataAdapter("Select * From books", "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Display Center Software\DisplayCenterData.mdb")
        daBook.Fill(dtBook)

        DataGridBook.DataSource = dtBook

        txtBookID.DataBindings.Add("text", dtBook, "BookID")
        txtNameOfBook.DataBindings.Add("text", dtBook, "NameOfBook")
End Sub


‘for moving to Previous Record in Datatable (also in Datagrid) I put following Code

Private Sub nxtButton_Click()
Me.BindingContext(dtBook).Position -= 1

    End Sub

‘For moving to Next Record in datatable (also in datagrid) I put following code

Private Sub NextButton_Click()        Me.BindingContext(dtBook).Position += 1

    End Sub

‘for first Record I put this code
Private Sub FirstButton_Click()
        MaxRowsBook = dtBook.Rows.Count
        Me.BindingContext(dtBook).Position = 0
    End Sub

‘for last Record I put following code
Private Sub lastButton_Click()
        MaxRowsBook = dtBook.Rows.Count
        Me.BindingContext(dtBook).Position = MaxRowsBook
    End Sub

‘Finaly On Search Button I put following Code

Private Sub SearchButton_Click()
        srch = txtSearch.Text
        daBook = New OleDbDataAdapter("Select * From Books where NameOfBook like'" & srch & "%'", "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Display Center Software\DisplayCenterData.mdb")

dtBook = New DataTable(2)

        daBook.Fill(dtBook)
        DataGridBook.DataSource = dtBook

        ClearBookTable()



    End Sub

I m very much Happy that Above Coding Hints of Adatapost Solved a lot of my problems.

Here I Need a little more Help from Adatapost or any body else that how can I Add, Delete, Edit and Update Record specially using DataTable Feature, Can Any Body Please Help, Yes Above Code is Very Much Helpful and Easy understandable for Vb.net Database Users like me.

So Please Help Me that how to Add, Delete, Edit and Update Record from Database Table (.mdb) using Datatable Function

With Best Wishes
Fahad Memon