?
Solved

VB.NET 2005 Datagrid update problem

Posted on 2007-12-02
5
Medium Priority
?
1,126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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