Link to home
Start Free TrialLog in
Avatar of Marc333
Marc333

asked on

Updating the Dataset

Hello experts,
I've got a pretty simple one, "I think".  I originally created a form with a datagrid based off of a DataAdapter/Dataset created by the wizard.  Along with that, I had an "Update" button and everything worked.  Since then, I moved the DataAdapter/DataSet into the code for more control.  However, now my Update button isn't working and I'm having a hard time fixing it.  Any ideas?  Here's the code I'm using currently:

    Private Sub Updatebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Updatebtn.Click

        Dim strConnectionString1 As String = _
          "Provider = Microsoft.Jet.OLEDB.4.0;" & _
         "Data Source = C:\progr\progr_be.mdb;"
            Dim strsqlA = _
            "SELECT * FROM Actives;"


            Dim objConnection1 As New OleDb.OleDbConnection(strConnectionString1)
            Dim OleDBDataAdapter1 As New OleDb.OleDbDataAdapter(strsqlA, objConnection1)
            Dim ActivesDS1 As New DataSet()
        '           OleDBDataAdapter1.Fill(ActivesDS1, "Actives")
        '            DataGrid1.DataSource = ActivesDS1.Tables(0)


        If ActivesDS1.HasChanges Then
            OleDBDataAdapter1.Update(ActivesDS1)
        End If

    End Sub
ASKER CERTIFIED SOLUTION
Avatar of maralans
maralans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marc333
Marc333

ASKER

well, I'm getting the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: Update unable to find TableMapping['Actives'] or DataTable 'Actives'.

Any thoughts?
Is it filling the datagrid?  You have that commented out in your question.
Also, do you have a primary key set?
Avatar of Marc333

ASKER

The datagrid is filling.  Actually, the datagrid is filled on the load event.  And to be specific, what happens with the original code is that the data is changed and it looks ok, but when I leave the form and come back, its the old data.  

Also, there is a primary key called "ID".  
Avatar of Marc333

ASKER

By the way, the reason I don't get an error on the original code is that the program is not seeing the HasChanges event triggered, so it skips the Update command.  If I take out the "IF" portion, it gets the error as well.  
Is your connection commands the exact same in the load event as they are in the save event?
Avatar of Marc333

ASKER

Yes they are the same.  One thing thats kind of interesting is that, I uncommented the Fill and Datasource comments, and now, when I push the button, it goes back to the old data.  I imagine that its refilling it with the old data.  
Okay, try this:

Put list at the top of your form:

Dim strConnectionString1 As String = _
              "Provider = Microsoft.Jet.OLEDB.4.0;" & _
             "Data Source = C:\progr\progr_be.mdb;"
    Dim strsqlA = "SELECT * FROM Actives;"
    Dim objConnection1 As New OleDb.OleDbConnection(strConnectionString1)
    Dim OleDBDataAdapter1 As New OleDb.OleDbDataAdapter(strsqlA, objConnection1)
    Dim ActivesDS1 As New DataSet()

Then in the load event put this:

OleDBDataAdapter1.Fill(ActivesDS1, "Actives")
DataGrid1.DataSource = ActivesDS1.Tables(0)

Then in your save event put this:

Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(OleDBDataAdapter1)
OleDBDataAdapter1.Update(ActivesDS1, "Actives")


I hope I am not leading you down the wrong path.  I haven't had the need to connect to access in a while.

I think the problem is you created a dataset on the load event and then another one by the same name in the save event and then checked for changes and there weren't any because it was a brand new dataset.
Avatar of Marc333

ASKER

Where do I put the first part?  Does that go in the "Region" area?
DataGrid1.DataSource = ActivesDS1.Tables("Actives")
Should look something like this:

Public Class Form1
    Dim strConnectionString1 As String = _
              "Provider = Microsoft.Jet.OLEDB.4.0;" & _
             "Data Source = C:\progr\progr_be.mdb;"
    Dim strsqlA = "SELECT * FROM Actives;"
    Dim objConnection1 As New OleDb.OleDbConnection(strConnectionString1)
    Dim OleDBDataAdapter1 As New OleDb.OleDbDataAdapter(strsqlA, objConnection1)
    Dim ActivesDS1 As New DataSet()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OleDBDataAdapter1.Fill(ActivesDS1, "Actives")
        DataGridView1.DataSource = ActivesDS1.Tables("Actives")
   End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(OleDBDataAdapter1)
        OleDBDataAdapter1.Update(ActivesDS1, "Actives")
    End Sub
End Class
Avatar of Marc333

ASKER

Well, I think this is very close.  I'm still getting an error message
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

But otherwise, its loading and doing all the things its supposed to.   Also the Has Changes event is now being triggered correctlly.  
Imports System
Imports System.Data
Imports System.Data.Oledb

Put this at the very top of your form
Avatar of Marc333

ASKER

Hmm.. no dice.   Still got the message...
Post your code.
Avatar of Marc333

ASKER

' I did get delete out the Regions portion which expanded automatically.  

Imports System
Imports System.Data
Imports System.Data.Oledb

Public Class Analysisfm
    Inherits System.Windows.Forms.Form




    Dim strConnectionString1 As String = _
                   "Provider = Microsoft.Jet.OLEDB.4.0;" & _
                  "Data Source = C:\progr\progr_be.mdb;"
    Dim strsqlA = "SELECT * FROM Actives;"
    Dim objConnection1 As New OleDb.OleDbConnection(strConnectionString1)
    Dim OleDBDataAdapter1 As New OleDb.OleDbDataAdapter(strsqlA, objConnection1)
    Dim ActivesDS1 As New DataSet()




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        OleDBDataAdapter1.Fill(ActivesDS1, "Actives")
        DataGrid1.DataSource = ActivesDS1.Tables("Actives")

    End Sub


    Private Sub Updatebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Updatebtn.Click

        If ActivesDS1.HasChanges Then MsgBox("here")
        Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(OleDBDataAdapter1)
        OleDBDataAdapter1.Update(ActivesDS1, "Actives")


    End Sub

Avatar of Marc333

ASKER

One thought did come to mind.  When I had the OleDBDataAdapter/DataSet created by the Wizard, I do believe that the program automatically created the Update Queries.  Do you think that the program was able to make the update because of those generated queries?
What else does the error say?
Avatar of Marc333

ASKER

This is the entire message:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll


I just used this code and it worked fine.


Public Class Form1
    Dim strConnectionString1 As String = _
              "Provider = Microsoft.Jet.OLEDB.4.0;" & _
             "Data Source = C:\program files\contractors.mdb;"
    Dim st = "SELECT Name FROM Allcontractors;"
    Dim conn As New OleDb.OleDbConnection(strConnectionString1)
    Dim da As New OleDb.OleDbDataAdapter(st, conn)
    Dim ds As New DataSet()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        da.Fill(ds, "Contractors")
        DataGridView1.DataSource = ds.Tables("Contractors")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(da)
        da.Update(ds, "Contractors")
    End Sub
End Class
How may columns in the table
Avatar of Marc333

ASKER

Hmm.. Well there are 22 columns total.  

Tell you what.  I'm going to call it a night and revisit this again in the morning. I'll try re-entering the code, or I may try building it with everything the exact same as you've got above.  If that doesn't work, I may have to come up with another work-around solution.  

I'm going to go ahead and award the points because of all the help.  Thanks again, I really appreciate it.

Marc
Try this.  It will only fill the dataset with the primary key.  It may be throwing the exception if there are empty fields in some of your columns.


Imports System
Imports System.Data
Imports System.Data.Oledb

Public Class Analysisfm
    Inherits System.Windows.Forms.Form

    Dim strConnectionString1 As String = _
                   "Provider = Microsoft.Jet.OLEDB.4.0;" & _
                  "Data Source = C:\progr\progr_be.mdb;"
    Dim strsqlA = "SELECT ID FROM Actives;"
    Dim objConnection1 As New OleDb.OleDbConnection(strConnectionString1)
    Dim OleDBDataAdapter1 As New OleDb.OleDbDataAdapter(strsqlA, objConnection1)
    Dim ActivesDS1 As New DataSet()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        OleDBDataAdapter1.Fill(ActivesDS1, "Actives")
        DataGrid1.DataSource = ActivesDS1.Tables("Actives")

    End Sub

    Private Sub Updatebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Updatebtn.Click

        If ActivesDS1.HasChanges Then MsgBox("here")
        Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(OleDBDataAdapter1)
        OleDBDataAdapter1.Update(ActivesDS1, "Actives")


    End Sub
Avatar of Marc333

ASKER

Maralans,
I'll give that a try tonight and let you know how it goes.  You're absolutely right though about the empty values.  I have several columns where null values are very typical.