Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 2nd, 2004, 07:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
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.

  #2 (permalink)  
Old August 2nd, 2004, 07:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
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
  #3 (permalink)  
Old August 2nd, 2004, 02:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
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.

  #4 (permalink)  
Old August 3rd, 2004, 03:25 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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. ;)
  #5 (permalink)  
Old August 3rd, 2004, 03:56 AM
Friend of Wrox
 
Join Date: Apr 2004
Location: Preston, Lancashire, United Kingdom.
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
  #6 (permalink)  
Old August 3rd, 2004, 04:09 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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.
  #7 (permalink)  
Old August 3rd, 2004, 04:11 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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>
  #8 (permalink)  
Old August 3rd, 2004, 05:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
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
  #9 (permalink)  
Old August 3rd, 2004, 08:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
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.

  #10 (permalink)  
Old August 3rd, 2004, 08:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
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>
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:21 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.