473,407 Members | 2,306 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,407 software developers and data experts.

How do I read back autonumber after do an insert in asp.net

Is there any way using a 'datareader' to find out the autonumber field
value of a record that I've just inserted?
Thanks,
-- cohenmarvin

Nov 19 '05 #1
10 1384
Well it depends on what database you are using. If you are using SQL Server
this should work:

SELECT @@Identity

--
T. DAVIS JR

- .NET will change the way developers develop applications
"COHENMARVIN" <co*********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Is there any way using a 'datareader' to find out the autonumber field
value of a record that I've just inserted?
Thanks,
-- cohenmarvin

Nov 19 '05 #2
COHENMARVIN wrote:
Is there any way using a 'datareader' to find out the autonumber field
value of a record that I've just inserted?
Thanks,
-- cohenmarvin


You're best bet is to avoid using autonumber in the first place.
Life is MUCH easier if you assign it yourself

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com
Nov 19 '05 #3
"Curt_C [MVP]" <software_at_darkfalz.com> wrote in news:#OTlPBfnFHA.2156
@TK2MSFTNGP14.phx.gbl:
COHENMARVIN wrote:
Is there any way using a 'datareader' to find out the autonumber field
value of a record that I've just inserted?
Thanks,
-- cohenmarvin


You're best bet is to avoid using autonumber in the first place.
Life is MUCH easier if you assign it yourself


??? Then you run into the possiblity of generating a duplicate...

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #4
"T. DAVIS JR" <so*****@hotmail.com> wrote in
news:ev**************@tk2msftngp13.phx.gbl:
Well it depends on what database you are using. If you are using SQL
Server this should work:

SELECT @@Identity

@@IDENTITY is potentially a BAD thing! SELECT SCOPE_IDENTITY() is a
better choice since @@identity could return the wrong value if there are
triggers on the table.

Example:

http://www.trigonblue.com/sp_identity.htm

Here's a table explaining the difference:

SELECT @@IDENTITY <- Session Scope
SELECT IDENT_CURRENT('tablename') <- Table scope
SELECT SCOPE_IDENTITY <- statement scope
In detail:

http://www.sqlteam.com/item.asp?ItemID=319

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #5
Well I agree, but for 95% of us that don't use triggers it shouldn't really
matter. However, selecting scope_identity() wouldn't hurt I guess.

--
T. DAVIS JR

- .NET will change the way developers develop applications
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@127.0.0.1...
"T. DAVIS JR" <so*****@hotmail.com> wrote in
news:ev**************@tk2msftngp13.phx.gbl:
Well it depends on what database you are using. If you are using SQL
Server this should work:

SELECT @@Identity

@@IDENTITY is potentially a BAD thing! SELECT SCOPE_IDENTITY() is a
better choice since @@identity could return the wrong value if there are
triggers on the table.

Example:

http://www.trigonblue.com/sp_identity.htm

Here's a table explaining the difference:

SELECT @@IDENTITY <- Session Scope
SELECT IDENT_CURRENT('tablename') <- Table scope
SELECT SCOPE_IDENTITY <- statement scope
In detail:

http://www.sqlteam.com/item.asp?ItemID=319

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 19 '05 #6
"T. DAVIS JR" <so*****@hotmail.com> wrote in
news:O1**************@TK2MSFTNGP10.phx.gbl:
Well I agree, but for 95% of us that don't use triggers it shouldn't
really matter. However, selecting scope_identity() wouldn't hurt I
guess.


It's not only triggers... if you do two inserts in a row, it could return
the wrong value ; )

And yes, it's better to be safe than sorry! I found out the hardway
wondering why my identity fields were all wrong ; )

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #7
Lucas Tam wrote:
"Curt_C [MVP]" <software_at_darkfalz.com> wrote in news:#OTlPBfnFHA.2156
@TK2MSFTNGP14.phx.gbl:

COHENMARVIN wrote:
Is there any way using a 'datareader' to find out the autonumber field
value of a record that I've just inserted?
Thanks,
-- cohenmarvin


You're best bet is to avoid using autonumber in the first place.
Life is MUCH easier if you assign it yourself

??? Then you run into the possiblity of generating a duplicate...

true... but if you put in a proper id generator you wont have this
issue. Very few "professional" apps use the auto generator, they ID it
themselves.

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com
Nov 19 '05 #8
"Curt_C [MVP]" <software_at_darkfalz.com> wrote in news:#3kzrEinFHA.2484
@TK2MSFTNGP15.phx.gbl:
??? Then you run into the possiblity of generating a duplicate...

true... but if you put in a proper id generator you wont have this
issue. Very few "professional" apps use the auto generator, they ID it
themselves.


Of course there are situations where this maybe needed - but for the
average application how much do you gain from rolling your own generator?
Building your own id generator means you'll need to maintain state, insure
that IDs are unique, concurrency issues - too much work especially when any
decent database already does it for you.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #9
Prevention is better that cure..
But at the same time if your not inserting 2 rows
then the latter should be fine

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@127.0.0.1...
"T. DAVIS JR" <so*****@hotmail.com> wrote in
news:O1**************@TK2MSFTNGP10.phx.gbl:
Well I agree, but for 95% of us that don't use triggers it shouldn't
really matter. However, selecting scope_identity() wouldn't hurt I
guess.


It's not only triggers... if you do two inserts in a row, it could return
the wrong value ; )

And yes, it's better to be safe than sorry! I found out the hardway
wondering why my identity fields were all wrong ; )

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 19 '05 #10
Avoiding the autogenerator issue, I would simply create a Stored Procedure
that selects the records and returns the ID (however generated) as an output
parameter.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@127.0.0.1...
"Curt_C [MVP]" <software_at_darkfalz.com> wrote in news:#3kzrEinFHA.2484
@TK2MSFTNGP15.phx.gbl:
??? Then you run into the possiblity of generating a duplicate...

true... but if you put in a proper id generator you wont have this
issue. Very few "professional" apps use the auto generator, they ID it
themselves.


Of course there are situations where this maybe needed - but for the
average application how much do you gain from rolling your own generator?
Building your own id generator means you'll need to maintain state, insure
that IDs are unique, concurrency issues - too much work especially when
any
decent database already does it for you.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 19 '05 #11

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

Similar topics

3
by: Ilan Sebba | last post by:
I have a 'supertype' table with only one field: autonumber. Call this table the 'parent' table. There are two subtypes, 'androids' and 'martians'. Martian have only one thing in common: they give...
12
by: Steve Jorgensen | last post by:
Hi all, I've migrated an Access database schema to PostgreSQL, and I'm trying to get everything working again. I'm having the following problem... Within an Access transaction, I insert a row...
8
by: bigbinc | last post by:
I am in autonumber hell, it is important for me to get the AutoNumber even in case of previous deleted records, and I cant get the value through a 'test' insert and then delete method. There is no...
3
by: ben.werdmuller | last post by:
Hi, Is there an easy way in ASP/VBscript to grab an autonumber (primary key) field just after an SQL insert? This is probably easy, but I'm stuck .. Cheers.
1
by: loreille | last post by:
To insert a record in a Ms Access database and be able to retrieve the newly created ID (autonumber) I used the code below (code 1). Now, the problem is that this is not very secure and that, if...
8
by: petebeatty | last post by:
I have created a SQL string the properly inserts a record in the table. However, the insert does not occur at the end of the table. Instead it inserts a record after the last record that I viewed....
1
by: jason.teen | last post by:
Hi, Curerntly I have designed my table in MS Access with a column of "RuleID" and is set to (autonumber) tblRule: RuleID | Name ------------------------------- 1 ...
12
by: magmike | last post by:
Accidentally deleted a record. Anyway to get it back? If not, I know the ID number - which is an autonumber field. Because of the related data from other tables, would I be able to create a new...
6
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to register on the website, they fill out a form and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.