 |
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 2nd, 2004, 07:46 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Retrieving the AutoNumber
Is it possible to retrieve the Autonumber of the last INSERT statement? I know MS SQL Server can do it but I'm not sure if Access can.
|

August 2nd, 2004, 07:54 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Just after INSERT, use a SELECT Max(AutoNumber_Field), that should return the one added. But remember, if someone adds another before you do the Select Max(), you would get the value of that row.
Cheers!
_________________________
- Vijay G
Strive for Perfection
|

August 2nd, 2004, 02:42 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
I found this guide in the MSDN for VS.NET 2003 but I'm not using a DataAdapter nor a DataSet.
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconretrievingidentityorautonumbervalues.htm
I tried to use ExecuteScalar() to return the first column of the first row and store it into a variable like this:
myResult = dbComm.ExecuteScalar()
later in the code:
dbComm2.Parameters("whatever").Value = myResult
but it keeps returning 0.
What I'm trying to do is insert values into a table and take the AutoNumber value from the INSERT statement and then insert some more values into another table with the AutoNumber value from the first INSERT as the FK to the last table.
|

August 3rd, 2004, 03:25 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
You must be very careful using MAX() because what Vijay mentioned.
When I run an insert from which I need to get the identity value, I do this:
INSERT INTO MyTable(...) VALUES(...); SELECT @@IDENTITY;
Then I execute it with ExecuteScalar(). Because both queries are running in the same command connection, it will accurately return the correct value.
I'm not sure about doing this with Access, but you shouldn't be using Access for a web application anyway. ;)
|

August 3rd, 2004, 03:56 AM
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why shouldnt you use an access database for a web application? (I know I am having lots of difficulties with mine, but i think thats because its allnew to me).
David
|

August 3rd, 2004, 04:09 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Oh let me count the ways! ;)
Well, access databases were designed as standalone databases. They can't handle many concurrent users. More importantly, they can only handle a single updater because of the file lock placed on the .MDB file when the database is being accessed. You'll get file access errors and the like. For a web site of more than a couple users, it's not a good solution. An enterprise database system (i.e.: MS-SQL, Oracle, MySQL etc.) is much preferred and supports infinately greater scalability.
I'm sure there are many more reasons, but I very rarely use access so I'm not an authoritative resource.
|

August 3rd, 2004, 04:11 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
<analogy>
Access: drinking straw
Enterprise RDBMS: Fire hose
Which would you rather water your grass with?
</analogy>
|

August 3rd, 2004, 08:12 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Access 2000 and later does support @@Identity. And I do plan on upgrading to SQL Server a couple of months down the road.
|

August 3rd, 2004, 08:13 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
I like a challenge :)
Quote:
quote:Originally posted by planoie
<analogy>
Access: drinking straw
Enterprise RDBMS: Fire hose
Which would you rather water your grass with?
</analogy>
|
|
|
 |