INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

HOW TO Update Access database from DataGridView using OleDbDataAdapter & OleDbCommand

HOW TO Update Access database from DataGridView using OleDbDataAdapter & OleDbCommand

HOW TO Update Access database from DataGridView using OleDbDataAdapter & OleDbCommand

(OP)
I have been struggling with the below code trying to get it to work. I have never used the OleDbDataAdapter & OleDbCommand and am not sure what I am doing. Keep getting this err msg at the high-lighted code, "Object reference not set to an instance of an object". I must have more than one thing wrong. I copied this code from the internet. Also don't know what to put in those ? marks.
I am using Access 2010 and vs2013 express.

Imports System.Data.OleDb
Public Class Form1
Dim myDA As OleDbDataAdapter
Dim myDataSet As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\test.mdb") ' Use relative path to database file

Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1", con)

con.Open()

myDA = New OleDbDataAdapter(cmd)

'Here one CommandBuilder object is required.

myDA.UpdateCommand = New OleDbCommand("UPDATE Table1 SET Field1 = ?, Field2 = ?")


'It will automatically generate DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object

Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)

myDataSet = New DataSet()

myDA.Fill(myDataSet, "Table1")

DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView

con.Close()

con = Nothing

End Sub



' Save data back into database

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.Validate()

Me.myDA.Update(Me.myDataSet.Tables("MyTable"))

Me.myDataSet.AcceptChanges()

End Sub
End Class


RE: HOW TO Update Access database from DataGridView using OleDbDataAdapter & OleDbCommand


First, how are you getting this to actually load? This line in Form_Load should be throwing an object reference error:

DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView

In the previous line, you name the table in the dataset "Table1":

myDA.Fill(myDataSet, "Table1")

Then you try to access it with "MyTable". I copied this code into a project, ran it and the "DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView" code threw an error. So either change Table1 to MyTable, or vice versa. Then in the button click sub, make that reference the same.

The question marks are just placeholders for the data that will be used to update the database. You don't actually put anything in there.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: HOW TO Update Access database from DataGridView using OleDbDataAdapter & OleDbCommand

(OP)
Hi jebenson,
I have been away for awhile. Your last reply showed me my sloppy programming.
Thank You for your input.
faxpay

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close