Wrox Programmer Forums
|
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
 
Old August 2nd, 2004, 07:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default 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.

 
Old August 2nd, 2004, 07:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old August 2nd, 2004, 02:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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.

 
Old August 3rd, 2004, 03:25 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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. ;)
 
Old August 3rd, 2004, 03:56 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default

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
 
Old August 3rd, 2004, 04:09 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 
Old August 3rd, 2004, 04:11 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

<analogy>
Access: drinking straw
Enterprise RDBMS: Fire hose

Which would you rather water your grass with?
</analogy>
 
Old August 3rd, 2004, 05:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi stu9820,

So here is the answer for your query. This is done with ASP, but still I believe the ADO concepts haven't changed vastly with the ADO.Net, here is the link - How To Return Record's Autonumber Value Inserted into Access DB

And regarding Planoie's comment on Access NOT best fit for web applications, here are some specifications/limitations of access.

Specifications for an Access database and for an Access Project

Access 97 - Some common Access specifications

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old August 3rd, 2004, 08:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Access 2000 and later does support @@Identity. And I do plan on upgrading to SQL Server a couple of months down the road.

 
Old August 3rd, 2004, 08:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Autonumber problem mankoti_mankoti2000 Access 6 August 29th, 2006 12:28 AM
AutoNumber causes problem. myself Classic ASP Professional 1 July 10th, 2006 03:35 PM
regarding Autonumber... MuthuAL Classic ASP Databases 2 December 8th, 2004 08:04 AM
Access Autonumber chall90909 Access ASP 1 November 21st, 2004 10:26 AM
AutoNumber Conundrum enterbase Access VBA 4 January 28th, 2004 05:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.