Access DataAdapter exception

J

John Dann

I've moved some code for updating an Access database from an old
project (no longer to hand unfortunately) to a new one and keep
getting an 'Update requires a valid InsertCommand when passed DataRow
collection with new rows'.

The Access database is a newly created one with no preexisting
datarows. (I can see this OK from within Access and the Table name,
column names etc are all OK.)

Code outline is as follows:

--------------------------------
dim ds as new dataset

Function MyFunction
ds.Clear() 'MyFunction may be called iteratively
Dim WLDBConn As New OleDbConnection(WLDBConnString)
Dim da As New OleDbDataAdapter(WLDBGenOpen, WLDBConn)
da.FillSchema(ds, SchemaType.Source)

[ Populate ds]
For i = 1 to whatever
Dim dr As DataRow = ds.Tables(0).NewRow
'Assign column values here
ds.Tables(0).Rows.Add(dr)
Next i

da.Update(ds)
End Function
-----------------------------

The error's generated at the da.update line.

The WLDBGenOpen and WLDBConnString strings work in other functions of
the project without problem so I don't think they're at fault. After
the 'Populate ds' process, I can bind the resulting ds to a grid and
all looks fine. So there's something in the Update syntax or whatever
that's faulty.

Any thoughts please?

JGD
 
G

Grzegorz Danowski

U¿ytkownik "John Dann said:
I've moved some code for updating an Access database from an old
project (no longer to hand unfortunately) to a new one and keep
getting an 'Update requires a valid InsertCommand when passed DataRow
collection with new rows'. (...)

--------------------------------
dim ds as new dataset

Function MyFunction
ds.Clear() 'MyFunction may be called iteratively
Dim WLDBConn As New OleDbConnection(WLDBConnString)
Dim da As New OleDbDataAdapter(WLDBGenOpen, WLDBConn)
da.FillSchema(ds, SchemaType.Source)

[ Populate ds]
For i = 1 to whatever
Dim dr As DataRow = ds.Tables(0).NewRow
'Assign column values here
ds.Tables(0).Rows.Add(dr)
Next i

Please add one line:
Dim cb As New OleDbCommandBuilder(da)
da.Update(ds)
End Function
(...)

Simply ADO.NET works in some other way that old ADO - you can explicitly
pass UpdateCommand or (less recomended because of less efficiency) you can
use CommandBuilder object.

Regards,
Grzegorz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top