Autoincrement database tavle field

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
 
D

Darrin J Olson

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top