By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Can't update an Access database

P: n/a
Hi all,

This is my first attempt to update an access database in VB.Net 2005. It
goes broadly as follows (I have condensed it):

Dim MyConnection As New Data.OleDb.OleDbConnection
MyConnection.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\books.mdb")
Dim Myadaptor As OleDb.OleDbDataAdapter
Myadaptor = New OleDb.OleDbDataAdapter("select * from stockitems",
MyConnection)
Dim MyDataSet As New DataSet
MyConnection.Open()
Myadaptor.Fill(MyDataSet)
Dim newRow As DataRow = MyDataSet.Tables(0).NewRow
newRow("code") = "New Code"
MyDataSet.Tables(0).Rows.Add(newRow)
Myadaptor.Update(MyDataSet)
MyConnection.Close()

I have tried a few ways to do this but I always get an error:
"Update requires a valid InsertCommand when passed DataRow collection with
new rows."

Any ideas? Any help gratefully received.

Mark
Feb 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Mrk Blackall" <Mrk Bl******@discussions.microsoft.comwrote in message
news:37**********************************@microsof t.com...
Hi all,

This is my first attempt to update an access database in VB.Net 2005. It
goes broadly as follows (I have condensed it):

Dim MyConnection As New Data.OleDb.OleDbConnection
MyConnection.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\books.mdb")
Dim Myadaptor As OleDb.OleDbDataAdapter
Myadaptor = New OleDb.OleDbDataAdapter("select * from stockitems",
MyConnection)
Dim MyDataSet As New DataSet
MyConnection.Open()
Myadaptor.Fill(MyDataSet)
Dim newRow As DataRow = MyDataSet.Tables(0).NewRow
newRow("code") = "New Code"
MyDataSet.Tables(0).Rows.Add(newRow)
Myadaptor.Update(MyDataSet)
MyConnection.Close()

I have tried a few ways to do this but I always get an error:
"Update requires a valid InsertCommand when passed DataRow collection with
new rows."

Any ideas? Any help gratefully received.

Mark

Couldn't you use a SqlCommand (System.Data.SqlCommand?) and just execute an
INSERT INTO or UPDATE query?
Failing that, look into the properties of your DataSet and ensure you've
opened it for writing as well.
I'd suggest exact code, but I'm only just starting to get used to .NET, and
all the data projects I'm working on, I'm either pulling data from a
webservice, or a SQL2000 database.
I'd hazard a guess that there may be a 3rd option in the OleDbDataAdapter
constructor that would specify whether you're opening the databse for
reading only(adForwardOnly) or for writing back to it as well.
Also check the file permissions and make sure that you can actually write to
the database?

Hope some of this helps.

--Paul Evans CCNA
SHL Computing

Feb 17 '07 #2

P: n/a
ADO.net is unnecessarily complex

use a command, dipshit


On Feb 16, 4:11 pm, Mrk Blackall <Mrk
Black...@discussions.microsoft.comwrote:
Hi all,

This is my first attempt to update an access database in VB.Net 2005. It
goes broadly as follows (I have condensed it):

Dim MyConnection As New Data.OleDb.OleDbConnection
MyConnection.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\books.mdb")
Dim Myadaptor As OleDb.OleDbDataAdapter
Myadaptor = New OleDb.OleDbDataAdapter("select * from stockitems",
MyConnection)
Dim MyDataSet As New DataSet
MyConnection.Open()
Myadaptor.Fill(MyDataSet)
Dim newRow As DataRow = MyDataSet.Tables(0).NewRow
newRow("code") = "New Code"
MyDataSet.Tables(0).Rows.Add(newRow)
Myadaptor.Update(MyDataSet)
MyConnection.Close()

I have tried a few ways to do this but I always get an error:
"Update requires a valid InsertCommand when passed DataRow collection with
new rows."

Any ideas? Any help gratefully received.

Mark

Feb 17 '07 #3

P: n/a
You need the OleDBCommandBuilder that will automatically the insert, delete
and update commands.

Here is an example of updating using the commandbuilder from VS 2005 Help

Public Shared Function UpdateRows(connectionString As String, _
queryString As String, tableName As String) As DataSet

Dim dataSet As DataSet = New DataSet

Using connection As New OleDbConnection(connectionString)
Dim adapter As New OleDbDataAdapter()
adapter.SelectCommand = New OleDbCommand(queryString, connection)
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)

connection.Open()

adapter.Fill(dataSet, tableName)

' Code to modify data in DataSet here

' Without the OleDbCommandBuilder this line would fail.
adapter.Update(dataSet, tableName)
End Using

Return dataSet
End Function
"Mrk Blackall" <Mrk Bl******@discussions.microsoft.comwrote in message
news:37**********************************@microsof t.com...
Hi all,

This is my first attempt to update an access database in VB.Net 2005. It
goes broadly as follows (I have condensed it):

Dim MyConnection As New Data.OleDb.OleDbConnection
MyConnection.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\books.mdb")
Dim Myadaptor As OleDb.OleDbDataAdapter
Myadaptor = New OleDb.OleDbDataAdapter("select * from stockitems",
MyConnection)
Dim MyDataSet As New DataSet
MyConnection.Open()
Myadaptor.Fill(MyDataSet)
Dim newRow As DataRow = MyDataSet.Tables(0).NewRow
newRow("code") = "New Code"
MyDataSet.Tables(0).Rows.Add(newRow)
Myadaptor.Update(MyDataSet)
MyConnection.Close()

I have tried a few ways to do this but I always get an error:
"Update requires a valid InsertCommand when passed DataRow collection with
new rows."

Any ideas? Any help gratefully received.

Mark


Feb 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.