last inserted record

  • Thread starter Thread starter afsheen
  • Start date Start date
A

afsheen

hello

Iam inserting a record (booking) into a database using the
dataadapter.update method ..... is there any way fo getting the id of the
last inserted record into the database with out writing an sql query ... is
there anything in datasets ... i have to return the booking id of the
inserted record to the customer .....
thanks
 
When you create the InsertCommand object, append the text of the
SelectCommand to the SQL, with a Where clause to get the last updated row.

For example,

Select ID, InsertDate, Description From Table1 Where ID=@@Identity

Then ensure that the UpdatedRowSource property of the InsertCommand is set
to either FirstReturnedRecord or Both. This ensures that the data retrieved
from the select statement issued following the insert is used to refresh the
data table.

If you use the SqlDataAdaptor Wizard to create a DataAdaptor (using drag and
drop from the toolbox), the default code generated does exactly this
(although you can override it).

An irritating point is that the DataRow is not updated by the UpdateCommand
when an error takes place, so you cannot find out what the current data in
the database is when an optimistic concurrency check fails, unless you
separately query the database.

Hope this helps clarify,

Neil.
 
In follow-up to Neil McKechnie's comment, if you are using sql 2000,
take a look at scope_identity vs identity.
 
Back
Top