2

I am hitting an Oracle database using C# and OleDb, I have sucessfully pulled data from a query but I am unable to update the database using OleDbAdapter.Update(), my update code is below. ("Adapter" is a reference to the OleDbAdapter object used to sucessfully pull data out of the database.)

OleDbCommandBuilder builder = new OleDbCommandBuilder(Adapter);
Adapter.UpdateCommand = builder.GetUpdateCommand();
Adapter.UpdateCommand.Prepare();
Adapter.Update(ds);
ds.AcceptChanges();

I am currently getting a "Command was not prepared." error on the first line of the above code. Any suggestions and I would be very grateful.

(Edit: ds is the DataSet)

casperOne
  • 73,706
  • 19
  • 184
  • 253
Yends
  • 111
  • 3
  • 10
  • 1
    it's rather hard to tell without seeing what the build returns. Sometimes the builder is not able to build a command from your Select - can you give us both - the select-query and the generated Update? – Random Dev Aug 31 '11 at 16:19
  • How can I get a hold of the generated Update? Note that the code throws an exception on the first line (OleDbCommandBuilder builder = new OleDbCommandBuilder(Adapter);) and so Adapter.UpdateCommand is null beforehand. My select query is actually a stored procedure which pulls id, firstName and lastName from a simple table. – Yends Aug 31 '11 at 16:21
  • sorry missed the "first line" comment - don't know exactly but is the Select in the Adapter right? Please set a breakpoint and insprect the Adapter-object for those Commands. – Random Dev Aug 31 '11 at 16:27
  • The select is a stored procedure, and that pulls the correct data from the database, I have unit tested it thoroughly. Thanks for your replies. – Yends Sep 01 '11 at 08:15
  • 1
    ok - how is the CommandBuilder suppost to generate a update based on a stored procedure? You have to come up with such an update-expression yourself. – Random Dev Sep 01 '11 at 08:17
  • wow ok, I thought the commandbuilder looked at the dataset returned, but thinking about mroe complicated examples which aren't just a single table that kinda makes sense, thanks for your help. – Yends Sep 01 '11 at 08:21

1 Answers1

2

your version is not working because you query a stored procedure.

If you think of it: to work it needs all the information like keys and so on. What if you join tables in your stored procedure and select only a subset of rows without some important primary-keys?

In those cases you might need to write/get a stored procedure or query yourself and give it to the adapter.

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • Thanks, that dawned on me after your last comment, makes sense when you think about it. – Yends Sep 01 '11 at 15:48