 |
| 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
|
|
|
|

June 25th, 2010, 04:57 AM
|
|
Authorized User
|
|
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
|
|
random image
Hi there
I'm trying to pull out 5 random images from a SQL database using the following:
Set con = Server.CreateObject("ADODB.Connection")
con.Open Application("TORCString")
strSQL = "SELECT id, Title, imagename, imagename_m, Detail FROM tbl_gallery WHERE IsActive = 0 ORDER BY sequence"
Set rsImg = Server.CreateObject("ADODB.RecordSet")
rsImg.Open strSQL, con, 3, 1
if Not rsImg.EOF then
strtitle = rsImg("title")
strimage = rsImg("imagename")
strimagename_m = rsImg("imagename_m")
strdetail = rsImg("detail")
upperlimit = rsImg.RecordCount
lowerlimit = 1
Randomize
rsImg.AbsolutePosition = Int((upperlimit - lowerlimit + 5) * Rnd() + lowerlimit)
but it's only pulling out the first image of the recordset - not sure why it's not working...any help greatly appreciated.
Many thanks
Adam
|
|

June 25th, 2010, 05:10 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Try debugging the coder and see what value RecordCount has. Maybe it's -1?
You could also let SQL get random images using ORDER BY NewID()
Hope this helps,
Imar
|
|

June 25th, 2010, 05:15 AM
|
|
Authorized User
|
|
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
|
|
random image
Thanks Imar
When I response.write the recordcount I get 6?
response.Write(upperlimit)
Don't think I know how to get SQL to randomize images using ORDER By NewID()?
|
|

June 25th, 2010, 05:40 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
There are two reasons I can think of why this doesn't work: the recordset doesn't support it (can't recall what exactly 3, 1 means) or your random number is not OK. What happens when you assign the random number to a variable first and debug that?
To get random records from SQL Server, you can use NewID() which generates a random value for each record. Something like this should give you two random records:
SELECT TOP 2 id, Title, imagename, imagename_m, Detail FROM tbl_gallery WHERE IsActive = 0 ORDER BY NewID()
Imar
|
|

June 25th, 2010, 05:47 AM
|
|
Authorized User
|
|
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
|
|
random image
Imar
You're right - when I output the random value as a variable it returns -1 so I understand why it's not working but I don't understand why it returns -1?
thanks
Adam
|
|

June 25th, 2010, 05:58 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Neither do I ;-)
This:
Code:
upperlimit = 6
lowerlimit = 1
Randomize
Response.Write((Int((upperlimit - lowerlimit + 5) * Rnd() + lowerlimit))
should output positive numbers. Does it do that for you?
Another thing: you're reading from the recordset before you set the AbsolutePosition. Is that deliberate?
Imar
|
|

June 25th, 2010, 06:08 AM
|
|
Authorized User
|
|
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
|
|
random image
Imar
think I'm going to do it your other method - NewID()
thanks alot
Adam
|
|
 |