Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases 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 December 30th, 2004, 12:07 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default gettting last record from DB

Hey,

I'm just wondering how to get the last record of the db, I try the following and get an error....

strconn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
accessdb = server.mappath("../db/portalDB.mdb")
strconn=strconn & accessdb & ";"

query2 = "SELECT * from test"
set db = server.CreateObject("adodb.connection")
db.open strconn
set rs = db.execute(query2)

rs.movelast 'line 33
rs.moveprevious
lastid = rs("ID")

response.write (lastid)

And I get the following error...

Microsoft JET Database Engine (0x80040E24)
Rowset does not support fetching backward.
/portal/Carnet/ajouter.asp, line 33

Right now I have a crappy work around that works, that I just itterate through the whole db, but that has a bit of an overhead...

rs.movefirst
while not rs.eof
lastid = rs("ID")
rs.movenext
wend
response.write (lastid)

Any help would be great..

 
Old December 30th, 2004, 02:16 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default


in order to get last record change your query as follows:

SELECT TOP 1 * FROM Test Order BY ID DESC

Please do not use rs.MoveLast

 
Old December 30th, 2004, 02:34 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why rs.movelast is such a bad thing?

 
Old December 30th, 2004, 03:00 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

*rs.MoveLast* I was referring to your code, you do not need to use that. Also, iterating through the entire recordset for finding the last record is also not a good idea. (performance point of view).




 
Old December 31st, 2004, 01:40 PM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Default

Your moveprevious() caused an error because you used a 'forwardonly' cursor. you can fix that by declaring another type of cursor but you headed on the wrong track. If you wanted to get the last ID of a table, you can use the value of @@IDENTITY right after your last insert - this returns the last-inserted identity value. I'm assuming you're using an autoincrement column for your ID field.

Another way is to use SELECT MAX(your_id_column).

'hope it helps.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Add New Record in Access DB sidra Classic ASP Databases 1 June 2nd, 2008 01:13 PM
gettting data from gridview in edit mode text cont mahboob_sabir ASP.NET 2.0 Professional 1 May 8th, 2007 02:05 AM
move record to another table in same DB Dejitan Classic ASP Basics 2 January 3rd, 2007 07:50 AM
Is It Possible to write record to DB Dwizz VB.NET 2002/2003 Basics 1 May 6th, 2005 07:16 AM
How do I check a db before inserting a record? Lucy SQL Server ASP 3 April 25th, 2005 10:47 PM





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