Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

ASP.NET Forum


You are currently viewing our ASP.NET Forum as a guest. Please register to participate.
Login



Reply
Read a database backwards
Old 08-15-2007, 01:13 AM Read a database backwards
Extreme Talker

Posts: 176
Trades: 0
right now, I have this:

Code:
Do While not rsUpdates.EOF 

Response.Write ("<br>")
    Response.Write (rsUpdates("upuser"))
    Response.Write ("<br>")
    Response.Write (rsUpdates("upinfo"))
    Response.Write ("<br>") 
    
    rsUpdates.MoveNext
Loop
But that just displays every thing on the list, in order.
How can I get just the final 3?(and pu them in backward order?
Skeddles is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-15-2007, 02:10 AM Re: Read a database backwards
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Change the sort - probably order by ??? desc - in the query your recordset is based on, and if you're getting the information from Access or SQL Server, use Select Top 3 ???

If you wanted to go a more complex route, you could declare a variable, and increment it every time you call MoveNext, then change your loop condition to While Not rsUpdates.Eof And i < 3 ... an integer variable will start at zero, so depending on where you put the line and whether you change it to start at one, you might change that to less than four, but by default everything starts at zero.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by ForrestCroce; 08-15-2007 at 02:14 AM.. Reason: Clarify 'change the sort'
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 08-15-2007, 05:32 AM Re: Read a database backwards
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
One quick solution, not very efficient because you still return the entire recordset would be;
Code:
rsUpdates.MoveLast

for zz = 0 to 2 
with response
    .Write ("<br>")
    .Write (rsUpdates("upuser"))
    .Write ("<br>")
    .Write (rsUpdates("upinfo"))
    .Write ("<br>") 
rsUpdates.MovePrev
end with
next
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2007, 08:06 AM Re: Read a database backwards
Belfast_des's Avatar
Novice Talker

Posts: 5
Name: Des Smith
Trades: 0
Hi,

If you change your SQL statement to include:


SELECT TOP 3 * FROM tblName ORDER BY upuser DESC

This will order them in reverse order and then only display the top 3 results.
Hope this helps,
D
Belfast_des is offline
Reply With Quote
View Public Profile
 
Old 08-15-2007, 02:34 PM Re: Read a database backwards
Extreme Talker

Posts: 176
Trades: 0
chrishirst: Ive tried that, but I always get :

Rowset does not support fetching backward.

when I use movelast.


belfast: I have no clue what to do with that
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 08-15-2007, 02:51 PM Re: Read a database backwards
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Yep. You will need to use a dynamic cursor

rsUpdates.CursorLocation = adUseClient
rsUpdates.CursorType = adOpenDynamic
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2007, 04:51 PM Re: Read a database backwards
Extreme Talker

Posts: 176
Trades: 0
I just don't get it. Can someone tell me what's wrong?
http://bit-digita.com/
Code:
<%
'Dimension variables
Dim adoCon         'Holds the Database Connection Object
Dim rsUpdates    'Holds the recordset for the records in the database
Dim strSQL         'Holds the SQL query to query the database

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("datab/updates.mdb")
Set rsUpdates = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT tblupdates.upuser, tblupdates.upinfo, tblupdates.updatesID FROM tblupdates;"
rsUpdates.Open strSQL, adoCon

rsUpdates.CursorLocation = adUseClient
rsUpdates.CursorType = adOpenDynamic

rsUpdates.MoveLast
for zz = 0 to 2 
with response
    .Write ("<br>")
    .Write (rsUpdates("upuser"))
    .Write ("<br>")
    .Write (rsUpdates("upinfo"))
    .Write ("<br>") 
rsUpdates.MovePrev
end with
next

rsUpdates.Close

Set rsUpdates = Nothing
Set adoCon = Nothing
%>
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 08-15-2007, 05:07 PM Re: Read a database backwards
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
Change CursorLocation and CursorType to 3 (both of them). Hirst is using the adovbs.inc constants.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 08-15-2007, 05:21 PM Re: Read a database backwards
Extreme Talker

Posts: 176
Trades: 0
Quote:
Originally Posted by ADAM Web Design View Post
Change CursorLocation and CursorType to 3 (both of them). Hirst is using the adovbs.inc constants.
Im sorry, im not sure what you mean. You mean make them both equal to 3?
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 08-15-2007, 08:00 PM Re: Read a database backwards
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
actually cursortype should be 2
Code:
rsUpdates.CursorLocation = 3
rsUpdates.CursorType = 2

http://www.w3schools.com/ado/prop_rs_cursorlocation.asp

http://www.w3schools.com/ado/prop_rs_cursortype.asp

http://www.candsdesign.co.uk/article...ado-constants/
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2007, 08:32 PM Re: Read a database backwards
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
I see where you're setting the connection object to null, but is that the same as actually closing it?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 08-15-2007, 09:42 PM Re: Read a database backwards
Extreme Talker

Posts: 176
Trades: 0
It says "operation is not allowed while object is open."

Why?
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 08-15-2007, 11:27 PM Re: Read a database backwards
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
Because your order is backwards.
Code:
rsUpdates.CursorLocation = 3
rsUpdates.CursorType = 2
rsUpdates.Open strSQL, adoCon
You have to set properties for your recordset object before you open it.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 08-20-2007, 09:25 PM Re: Read a database backwards
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
I still think you're better off changing your query to "SELECT TOP 3* FROM TABLE WHERE CONDITION = TRUE ORDER BY SORT_CONDITION DESC" ... let the database engine do your work for you. That isn't just about laziness; the database will do what you ultimately want more efficiently than your code will.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to Read a database backwards
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42438 seconds with 12 queries