Question unable to update access data base

Tarsem

New member
Joined
Apr 18, 2009
Messages
2
Programming Experience
Beginner
Dear All,

I am new in vb.net. I have access database Name"krish" have table in database namely"punjab". in my form I drop textbox and command buttons I am able to read access database in form design and moving forward and backword. but when i make some change in textbox it does not go in database, mean not update in access database. i am posting my code here. i use the commandbuilder to update access database but its not working..


VB.NET:
Imports System.Data.OleDb
Public Class Form1
    Dim con As New OleDbConnection
    Dim inc As Integer
    Dim sql As String
    Dim ds As New DataSet
    Dim ad As OleDbDataAdapter
    Dim Command As New OleDb.OleDbCommand
    Dim max As Integer
    Dim row As System.Data.DataRow
    Dim C As Integer



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


36:     con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = e:\my project\krish.mdb"
        con.Open()
        sql = "SELECT * FROM Punjab"
        ad = New OleDbDataAdapter(sql, con)
        ad.Fill(ds, "punjab")
        max = ds.Tables("punjab").Rows.Count
        con.Close()

    End Sub
    Private Sub record()
        TextBox1.Text = ds.Tables("punjab").Rows(inc).Item(0)
        TextBox2.Text = ds.Tables("punjab").Rows(inc).Item(1)
        TextBox3.Text = ds.Tables("punjab").Rows(inc).Item(2)
        TextBox4.Text = ds.Tables("punjab").Rows(inc).Item(3)
        TextBox5.Text = ds.Tables("punjab").Rows(inc).Item(4)
        TextBox6.Text = ds.Tables("punjab").Rows(inc).Item(5)
        TextBox7.Text = ds.Tables("punjab").Rows(inc).Item(6)
        TextBox8.Text = ds.Tables("punjab").Rows(inc).Item(7)
        TextBox9.Text = ds.Tables("punjab").Rows(inc).Item(8)
        TextBox10.Text = ds.Tables("punjab").Rows(inc).Item(9)
        TextBox11.Text = ds.Tables("punjab").Rows(inc).Item(10)
        TextBox12.Text = ds.Tables("punjab").Rows(inc).Item(11)
        TextBox13.Text = ds.Tables("punjab").Rows(inc).Item(12)


    End Sub

    Private Sub nextbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextbtn.Click
        If inc <> max - 1 Then
            inc = inc + 1

            record()

        End If

    End Sub

    Private Sub prebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles prebtn.Click
        If inc > 0 Then
            inc = inc - 1
            record()

        Else
            inc = 0
            MsgBox("No More Record")

        End If

    End Sub

    Private Sub firstbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles firstbtn.Click
        If inc <> 0 Then
            inc = 0
            record()
        End If
        MsgBox("First Record")

    End Sub

    Private Sub lastbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lastbtn.Click
        If inc > -1 Then
            inc = max - 1
            record()
        End If

    End Sub

    Private Sub updatebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updatebtn.Click
        Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(ad)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
        con.Open()

        ad.Fill(ds, "punjab")

        '
        builder.GetUpdateCommand()

        ad.Update(ds, "punjab")
        cmd.ExecuteNonQuery()

        MsgBox("data updated")

        con.Close()


    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

    End Sub


End Class
 
Last edited by a moderator:
That's because when you want to save you are completely ignoring your current data and you're getting a whole new set of data from the database and then saving that. That new set of data doesn't contain any changes so no changes are saved. When it comes time to save data you do NOT call Fill because you do NOT want to retrieve any data from the database. You ONLY call Update because you ONLY want to save the current data.
 

Latest posts

Back
Top