Solved

VB.NET 2005 Datagrid update problem

Posted on 2007-12-02
5
1,118 Views
Last Modified: 2013-11-26
Trying to make the transistion from VB6 to VB.NET 2005. I am having trouble doing the most simple task of updating the DB via a datagridview. All I'm doing is populating a datagrid on a form (which works), make a few changes on the datagrid and have it update the db.  Here's my code that does not work (I am avoiding the wizards so I can figure out what's going on):

Public Class frmInventoryMaster

    Private da As OleDb.OleDbDataAdapter
    Private cn As New OleDb.OleDbConnection
    Private strConn As String
    Private strSQL As String
    Private dt As New DataTable

'Load the datagrid on the form - this works fine  
    Private Sub frmInventoryMaster_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\TEMP\inventory.mdb;"
        cn.ConnectionString = strConn
        cn.Open()
        strSQL = "SELECT * FROM InventoryMaster"
        da = New OleDb.OleDbDataAdapter(strSQL, cn)
        da.Fill(dt)
        DataGridView1.DataSource = dt

    End Sub

'Update the database with the changes made in the datagrid - this DOES NOT WORK
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Try
            da.Update(dt)
            MsgBox("Update successful")
        Catch ex As Exception
            MsgBox("Update failed")
        End Try


    End Sub
End Class

Thanks,
Ken
0
Comment
Question by:ken1010
  • 3
5 Comments
 
LVL 15

Expert Comment

by:JackOfPH
ID: 20393073
try dt.acceptchanges
0
 

Author Comment

by:ken1010
ID: 20393156
dt.acceptchanges
had no effect - db still does not update.
0
 

Author Comment

by:ken1010
ID: 20393161
I tried this
  dt.acceptchanges
   da.Update(dt)


and just dt.acceptchanges.

neither worked.
0
 
LVL 41

Accepted Solution

by:
graye earned 125 total points
ID: 20393468
Hang on... you're doing great... you just missed a single line of code.

By default, the DataAdapter doesn't know how to perform the Update() method... it has to be told how.  You do that will this simple line of code:

      Dim cb as  New OleDbCommandBuilder(da)

Just place this somewhere after the DataAdapter get's instaniated.

Take a look at the following article (and downloadable VB.Net source code)... it is right on target:  http://home.hot.rr.com/graye/Articles/SavingDataADO.htm
0
 

Author Closing Comment

by:ken1010
ID: 31412231
Graye,
Thanks so much - it worked perfectly! (good article too)

Ken
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Paging GridView 7 34
Code enhancement 5 12
Change data in datatable 8 16
Vb.net/SQL prevent a table from being deleted ever 7 21
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now