473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Autoincrement database tavle field

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

Nov 17 '05 #1
2 1379
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
"RA" <ro****@hotmail.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
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

Nov 17 '05 #2
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
"RA" <ro****@hotmail.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
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

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jaYPee | last post by:
I have a table in my sqlserver 2000 that has a field IDNO. i want this field to be my primary key. however i don't want this field to use the autoincrement feature. when i access this table from...
0
by: RA | last post by:
Hi I have the following datarow that I want to add to a datatable. DataRow newRow = ds.Tables.NewRow (); newRow = Cache.ToString (); newRow = Cache.ToString (); ds.Tables.Rows.Add...
0
by: Neil | last post by:
Hi, I'm getting some strange results using the autoincrement column on my datatable. I'm populating a datatable with data from my database and displaying this in a datagrid. The first time I get...
6
by: Dennis | last post by:
I have set a DataTable and one of the columns I set "AutoIncrement" to True. I then populate the Table by setting the columns to values then add the row to the table. I inadverently set the...
6
by: Michael | last post by:
I am trying to create an access database within Net 2003 using the ADOX library which works fine except when I try to add the AutoIncrement property to the ContactId column. I am experiencing a...
3
by: MP | last post by:
context: vb6/ ado / .mdb format / jet 4.0 (not using Access - ADO only) - creating tables via ADO (don't have access) - all tables have a primary key (PK) - many of the PK will become FK(Foreign...
1
by: Mike | last post by:
I have a form that has an embedded subform (Datasheet View) that are linked based on a 1-many ID field. I have a field (SET) in my embedded subform that I want to Autoincrement starting with...
4
by: Tim | last post by:
Hello All, I could use some help on an error that is just now popping it's head up. Seems that the autoincrement numeric has hit 32,767. The autoincrement is used in various locations in the...
3
by: Csaba Gabor | last post by:
Is there any way to detect, based strictly on querying the structure of a table/database whether there is an AUTOINCREMENT set? That is to say, without analyzing the original SQL creation...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.