Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys I am working on a college project.
I am looking forward to perform data manipulation operations such as delete & update.
I am using DataSet & OleDbDataAdapter .
If I perform the delete or update the changes are not immediately reflected in the database.
I can see the changes only if I close the application & again restart it again.

Please help me how to update datasource(database) using oledbdataadapter with dataset.

Thank you in advance

on form_load event:
connection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data source= D:\Project(Pritam)\Networking Banking System\ActualDatabase.mdb")
        sql_first = "Select * from ClientInfo "
        sql_second = "Select * from Client_Account_Status"
        connection.Open()
        adapter = New OleDbDataAdapter(sql_first, connection)
        adapter.Fill(dataset, "ClientInfo")
        adapter.SelectCommand.CommandText = sql_second
        adapter.Fill(dataset, "Client_Account_Status")
        adapter.Dispose()
        connection.Close()
        i = 0
        last_record = ((dataset.Tables("ClientInfo").Rows.Count) - 1)
        final()
end sub
Public Sub final()
        Try
            Dim title As String = dataset.Tables("ClientInfo").Rows(i).Item("Title")
            Dim first_name As String = dataset.Tables("ClientInfo").Rows(i).Item("First_Name")
            Dim last_name As String = dataset.Tables("ClientInfo").Rows(i).Item("Last_Name")
            txtAccountHolderName.Text = title & " " & first_name & " " & last_name
            txtAccountNumber.Text = dataset.Tables("ClientInfo").Rows(i).Item("Account_No")
            txtAccountType.Text = dataset.Tables("ClientInfo").Rows(i).Item("Account_Type")
            txtCurrencyType.Text = dataset.Tables("ClientInfo").Rows(i).Item("Currency_Type")
            txtAccountBalance.Text = dataset.Tables("Client_Account_Status").Rows(i).Item("Balance")
            txtSearch.Text = txtAccountNumber.Text
        Catch ex As Exception
            MsgBox("No records to Delete", MsgBoxStyle.Critical, "Notice!")
            frmServerMainMenu.Show()
            Me.Close()
        End Try
    End Sub


---EDIT kschuler: Copied your code from your comment to the question. It's much easier for people to help when they can see the code formatted inside the question instead of the comments where it can't format.
Posted
Updated 10-Mar-11 6:08am
v3
Comments
m@dhu 10-Mar-11 1:39am    
I can see the changes only if I close the application & again restart it again.
Are you seeing the updated values in front end or in database?
Pritam N. Bohra 10-Mar-11 5:08am    
I can't see the updated values in the front end.
I see the updated values only if I restart the application.

I will show you the code


on form_load event:

[code added to OP]
Sandeep Mewara 10-Mar-11 1:47am    
Share the code. Update question with it.
Pritam N. Bohra 10-Mar-11 5:08am    
Public Sub final()
Try
Dim title As String = dataset.Tables("ClientInfo").Rows(i).Item("Title")
Dim first_name As String = dataset.Tables("ClientInfo").Rows(i).Item("First_Name")
Dim last_name As String = dataset.Tables("ClientInfo").Rows(i).Item("Last_Name")

txtAccountHolderName.Text = title & " " & first_name & " " & last_name
txtAccountNumber.Text = dataset.Tables("ClientInfo").Rows(i).Item("Account_No")
txtAccountType.Text = dataset.Tables("ClientInfo").Rows(i).Item("Account_Type")
txtCurrencyType.Text = dataset.Tables("ClientInfo").Rows(i).Item("Currency_Type")
txtAccountBalance.Text = dataset.Tables("Client_Account_Status").Rows(i).Item("Balance")

txtSearch.Text = txtAccountNumber.Text
Catch ex As Exception
MsgBox("No records to Delete", MsgBoxStyle.Critical, "Notice!")
frmServerMainMenu.Show()
Me.Close()
End Try
End Sub

1 solution

How are you updating the database? Because the code you provided doesn't show how you are pushing the changes back to the database. When you create a dataset using the OleDbDataAdapter, it makes a static copy. It is in no way connected to the actual database. So, unless you're updating the Db somewhere else, I don't know how you're changes would even be there when you restart the application.

But, just updating the DataAdapter isn't the only thing you need to do. It's very important to set up the table mapping. Without it, the DataAdapter doesn't know how to update the database.

Here's some example code that I have used in the past:

VB
Dim openedConnectionString As Boolean = False
If Not connectionString.State = ConnectionState.Open Then
    connectionString.Open()
    openedConnectionString = True
End If

Dim insertionDS As New DataSet
Dim newDataAdapter As OleDbDataAdapter
Dim sql As String = "SELECT * FROM [" & ActiveProject.Identifier & " Populations] ORDER BY ID;"

newDataAdapter = New OleDb.OleDbDataAdapter(sql, connectionString)

'Create mapping
newDataAdapter.TableMappings.Add("Table", "Populations")
With newDataAdapter.TableMappings(0).ColumnMappings
    .Add("ID", "ID")
    .Add("Name", "Name")
    .Add("Char ID", "Char ID")
    .Add("Stratum", "Stratum")
    .Add("PopLevel", "PopLevel")
End With

'Make sure field names are bracketed
Dim cb As New OleDbCommandBuilder(newDataAdapter)
cb.QuotePrefix = " ["
cb.QuoteSuffix = "] "

'Add Table
newDataAdapter.Fill(insertionDS)

'Get last ID number to increment
Dim lastIndex As Integer = 0
With insertionDS.Tables("Populations")
    If .Rows.Count <> 0 Then
        lastIndex = .Rows(.Rows.Count - 1).Item("ID")
    End If

    'Insert record
    Dim newRow As DataRow = .Rows.Add
    newRow.BeginEdit()
    newRow.Item("ID") = lastIndex + 1
    newRow.Item("Name") = Population
    newRow.Item("Char ID") = ""
    newRow.Item("Stratum") = Stratum
    newRow.Item("PopLevel") = Level
    newRow.EndEdit()
End With

'Save changes
'Update and Delete aren't actually necessary for this example...but you may need to create them
newDataAdapter.UpdateCommand = cb.GetUpdateCommand
newDataAdapter.InsertCommand = cb.GetInsertCommand
newDataAdapter.DeleteCommand = cb.GetDeleteCommand
newDataAdapter.Update(insertionDS)

newDataAdapter.Dispose()

'If we opened the connectionstring, then close it
If openedConnectionString Then
    connectionString.Close()
End If
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900