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
LVL 3
Marc333Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

maralansCommented:
I believe you need to declare a commandbuilder such as:

Dim cb As oledb.oledbCommandBuilder = New oledb.oledbCommandBuilder(OleDBDataAdapter1)

and then change your update to:

OleDBDataAdapter1.Update(ActivesDS1, "Actives")

Hope this works.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marc333Author Commented:
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?
0
maralansCommented:
Is it filling the datagrid?  You have that commented out in your question.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

maralansCommented:
Also, do you have a primary key set?
0
Marc333Author Commented:
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".  
0
Marc333Author Commented:
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.  
0
maralansCommented:
Is your connection commands the exact same in the load event as they are in the save event?
0
Marc333Author Commented:
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.  
0
maralansCommented:
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.

0
maralansCommented:
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.
0
Marc333Author Commented:
Where do I put the first part?  Does that go in the "Region" area?
0
maralansCommented:
DataGrid1.DataSource = ActivesDS1.Tables("Actives")
0
maralansCommented:
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
0
Marc333Author Commented:
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.  
0
maralansCommented:
Imports System
Imports System.Data
Imports System.Data.Oledb

Put this at the very top of your form
0
Marc333Author Commented:
Hmm.. no dice.   Still got the message...
0
maralansCommented:
Post your code.
0
Marc333Author Commented:
' 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

0
Marc333Author Commented:
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?
0
maralansCommented:
What else does the error say?
0
Marc333Author Commented:
This is the entire message:

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


0
maralansCommented:
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
0
maralansCommented:
How may columns in the table
0
Marc333Author Commented:
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
0
maralansCommented:
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
0
Marc333Author Commented:
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.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.