Solved

VB.NET 2005 Datagrid update problem

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

809 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