Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO Update Access database from DataGridView using OleDbDataAdapter & OleDbCommand 1

Status
Not open for further replies.

faxpay

Programmer
Nov 30, 2004
119
US
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()

[highlight #FCE94F]Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
[/highlight]
Me.myDataSet.AcceptChanges()

End Sub
End Class
[highlight #FCE94F][/highlight]

 

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!
 
Hi jebenson,
I have been away for awhile. Your last reply showed me my sloppy programming.
Thank You for your input.
faxpay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top