Solved

VB.NET 2005 Datagrid update problem

Posted on 2007-12-02
5
1,124 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 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 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