?
Solved

Updating the Dataset

Posted on 2006-04-20
26
Medium Priority
?
251 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:Marc333
  • 14
  • 12
26 Comments
 
LVL 5

Accepted Solution

by:
maralans earned 1000 total points
ID: 16503897
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
 
LVL 3

Author Comment

by:Marc333
ID: 16503917
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
 
LVL 5

Expert Comment

by:maralans
ID: 16503950
Is it filling the datagrid?  You have that commented out in your question.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:maralans
ID: 16503952
Also, do you have a primary key set?
0
 
LVL 3

Author Comment

by:Marc333
ID: 16503975
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
 
LVL 3

Author Comment

by:Marc333
ID: 16503990
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
 
LVL 5

Expert Comment

by:maralans
ID: 16504028
Is your connection commands the exact same in the load event as they are in the save event?
0
 
LVL 3

Author Comment

by:Marc333
ID: 16504075
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
 
LVL 5

Expert Comment

by:maralans
ID: 16504134
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
 
LVL 5

Expert Comment

by:maralans
ID: 16504171
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
 
LVL 3

Author Comment

by:Marc333
ID: 16504175
Where do I put the first part?  Does that go in the "Region" area?
0
 
LVL 5

Expert Comment

by:maralans
ID: 16504188
DataGrid1.DataSource = ActivesDS1.Tables("Actives")
0
 
LVL 5

Expert Comment

by:maralans
ID: 16504202
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
 
LVL 3

Author Comment

by:Marc333
ID: 16504241
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
 
LVL 5

Expert Comment

by:maralans
ID: 16504246
Imports System
Imports System.Data
Imports System.Data.Oledb

Put this at the very top of your form
0
 
LVL 3

Author Comment

by:Marc333
ID: 16504256
Hmm.. no dice.   Still got the message...
0
 
LVL 5

Expert Comment

by:maralans
ID: 16504258
Post your code.
0
 
LVL 3

Author Comment

by:Marc333
ID: 16504273
' 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
 
LVL 3

Author Comment

by:Marc333
ID: 16504299
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
 
LVL 5

Expert Comment

by:maralans
ID: 16504321
What else does the error say?
0
 
LVL 3

Author Comment

by:Marc333
ID: 16504328
This is the entire message:

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


0
 
LVL 5

Expert Comment

by:maralans
ID: 16504407
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
 
LVL 5

Expert Comment

by:maralans
ID: 16504414
How may columns in the table
0
 
LVL 3

Author Comment

by:Marc333
ID: 16504471
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
 
LVL 5

Expert Comment

by:maralans
ID: 16504486
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
 
LVL 3

Author Comment

by:Marc333
ID: 16511408
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question