DataAdapter.UpdateCommand problem

S

Sébastien

I generated my UpdateCommand with a CommandBuilder. As I seen on this group,
the CommandBuilder don't do the same the DataAdapter wizard do to generate
the UpdateCommand.

Here is my problem :
1- I add datas in my database table:
I add a new row in my DataSet then DataAdapter.Update(DataSet)
2- I update my database table :
I change a value in the row then DataAdapter.Update(DataSet)

It crash and it give me an error message saying I can't update 0 row. The
property HasError of my DataSet is set to true when that message is
displayed.

The problem don't happen if I have at least one row of data in the table I
try to update before I start the application. But if the table was empty
ebfore I execute my software, then it will crash for sure. It is a strange
problem.

To try to fix it, I created a OleDbDataAdapter using the wizard (so it add a
control on the form instead beeing a hardcoded object). I create a
DataAdapter object in my code also. I create my Insert and Delete commands
with the CommandBuilder for that coded obeject. For the UpdateCommand
property, I set it to the UpdateCommand property of my OleDbDataAdapter on
the form. I execute the software and now all work perfectly.

I don't want to use OleDbDataAdater on my form. Is there anything I don't do
right ?

Here is my actual way to get my dataadapter :
Private Function GetDataAdapter(ByVal strTable As String, ByVal
strConnection As String) As OleDb.OleDbDataAdapter

Dim oldOleDbCommandBuilder As OleDb.OleDbCommandBuilder
Dim oldOleDbDataAdapter As OleDb.OleDbDataAdapter

oldOleDbDataAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM " &
strTable, strConnection)
oldOleDbCommandBuilder = New
OleDb.OleDbCommandBuilder(oldOleDbDataAdapter)

oldOleDbDataAdapter.InsertCommand =
oldOleDbCommandBuilder.GetInsertCommand
oldOleDbDataAdapter.UpdateCommand =
oldOleDbCommandBuilder.GetUpdateCommand
oldOleDbDataAdapter.DeleteCommand =
oldOleDbCommandBuilder.GetDeleteCommand

Return oldOleDbDataAdapter

End Function

Is that OK or do I need to do something more ? I tryed to set the
UpdateCommandText manually, ut it still don't work. I tryed to copy the
exact same parameters the DataAdapter wizard give me and it did not work
either.

If someone could help me, I would appreciate.

Thank you.
 
W

William Ryan

You don't need to set the commands manually using the Getx. Here's a
snippet from Bill Vaughn's article on Weaning Developers from the
CommandBuilder which is all you'll need to get it to work:

Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim cb As SqlCommandBuilder
cn = New SqlConnection("data source=demoserverÃ,Â.")
da = New SqlDataAdapter("SELECT Au_ID, au_lname, City FROM authors", cn)
cb = New SqlCommandBuilder(da) ' Build new CommandBuilder

However, go over to his site and read the whole article, it'll certainly
help you out (plus there's all sorts of other good stuff there)
 
K

kids

I shared the same problems.
Hope MS. would kindly post an acticle .Net & Access Database (1 complete
process) for people trying to catch up with .net tech like me.
 

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