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 June 25th, 2010, 04:57 AM
Authorized User
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default 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
 
Old June 25th, 2010, 05:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 25th, 2010, 05:15 AM
Authorized User
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default 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()?
 
Old June 25th, 2010, 05:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 25th, 2010, 05:47 AM
Authorized User
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default 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
 
Old June 25th, 2010, 05:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 25th, 2010, 06:08 AM
Authorized User
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default random image

Imar

think I'm going to do it your other method - NewID()

thanks alot

Adam





Similar Threads
Thread Thread Starter Forum Replies Last Post
Random value(in C#) Banishah ASP.NET 3.5 Basics 5 June 24th, 2009 01:26 PM
Random value(in C#) Banishah ASP.NET 2.0 Basics 5 June 24th, 2009 01:04 PM
random image display rajuru Beginning PHP 3 September 10th, 2004 09:42 AM
Random Numbers not so random... katsarosj ASP.NET 1.0 and 1.1 Basics 5 November 20th, 2003 12:55 AM





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