Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.NET 2005 Datagrid update problem

Posted on 2007-12-02
5
Medium Priority
?
1,127 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses

636 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