473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can't update an Access database

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
3 9301

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jimserac | last post by:
I had previously posted this in an Access forum with negative results so will try here. Although this question specifies an Access database, I also wish to accomplish this with a large MS SQL...
4
by: Kathleen Turner | last post by:
I have an Excel file that has list of serial numbers followed by a 4 digit code. I have an Access database that has the serial numbers but the field for the 4 digit code is either blank, has the 4...
3
by: intl04 | last post by:
I was told by an instructor of Oracle SQL that an MS Access database cannot be used to have several people enter records at about the same time, that the database will "freeze up" as a result -...
2
by: pisaac | last post by:
I am considering purchasing Visual Basic .net 2003 Standard Editon to do some development work with Web Forms and I need to know whether this edit will allow me to update an Access database (mdb)...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
4
by: George | last post by:
Hi all, I am having trouble with updating my data in an Access database. here is my code: Imports System.Data.OleDb Dim AppPath As String = Mid(Application.ExecutablePath, 1,...
2
by: Scotty | last post by:
I get stuck to write an update, insert and delete command, i am looking for some help to start Whats the best way to update 2 tables toe the database (Access) below my code used to load my...
2
by: DuncanIdaho | last post by:
Hi Apologies if this is similar to a (very) recent post. I was wondering if it is possible to execute an update query that contains a select statement on an MS access 2000 database. I have...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
7
by: emajka21 | last post by:
I have been working on creating a HR database. Everything is working fine there but now they want a training database. I basically need a few fields from the employee table and I need the full...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.