Autoincrement database tavle field

  • Thread starter Thread starter RA
  • Start date Start date
R

RA

Hi

I have the following datarow that I want to add to a datatable.
DataRow newRow = ds.Tables["Customers"].NewRow ();

newRow["ContactFirstName"] = Cache["FirstName"].ToString ();

newRow["ContactLastName"] = Cache["LastName"].ToString ();

ds.Tables["Customers"].Rows.Add (newRow);



After the row has been added I want to get the row column value of
CustomerId (This column is defined in the database as Autoincrement).

How can I get this value? I need this value in order to update the orders
table which has a CustomeId column.



Thanks,

Ron
 
Ron,

I had trouble with this a while ago, and there are a number of posts about
it that I had found. The way I ended up doing it (assuming you are using a
MS SQL database, too) is to create a transaction before I updated the
database. In that same transaction I run a select query: 'SELECT @@IDENTITY
AS [NewID]' to get the newly inserted autoincrement field, and insert it
into the row I added. If there are multiple rows being updated, after the
update I just re-query the datatable/dataset to get the values.

I think this worked better in the old ADO, and others in the newsgroup have
told me that a better way to do this may be coming, but I haven't seen
anything. If you find a better way, please post it.

-Darrin
 
Back
Top