PC Review


Reply
Thread Tools Rate Thread

Can't update an Access database

 
 
=?Utf-8?B?TXJrIEJsYWNrYWxs?=
Guest
Posts: n/a
 
      17th Feb 2007
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


 
Reply With Quote
 
 
 
 
Paul Evans
Guest
Posts: n/a
 
      17th Feb 2007

"Mrk Blackall" <Mrk (E-Mail Removed)> wrote in message
news:37518EBE-1231-40BA-9FAB-(E-Mail Removed)...
> 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

 
Reply With Quote
 
aaron.kempf@gmail.com
Guest
Posts: n/a
 
      17th Feb 2007
ADO.net is unnecessarily complex

use a command, dipshit




On Feb 16, 4:11 pm, Mrk Blackall <Mrk
Black...@discussions.microsoft.com> wrote:
> 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



 
Reply With Quote
 
William LaMartin
Guest
Posts: n/a
 
      17th Feb 2007
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 (E-Mail Removed)> wrote in message
news:37518EBE-1231-40BA-9FAB-(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Update Excel Database via Access Main Database with a script Finnbar Microsoft Excel New Users 2 3rd Nov 2008 07:24 PM
Script to Update a Excel Database whenever a Access Database is mo Finnbar Microsoft Access 5 3rd Nov 2008 06:03 PM
update from other access database =?Utf-8?B?TmlheiBBaG1hZA==?= Microsoft Access External Data 4 10th Nov 2007 07:13 PM
Trying to update the Access Database =?Utf-8?B?RGF2ZQ==?= Microsoft Access Security 6 5th Apr 2006 03:04 PM
Can not update a Access database yoramo Microsoft ADO .NET 22 7th Dec 2003 10:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:00 PM.