How to solve this problem?

M

Miguel Dias Moura

Hello,

I need to display a random image in an ASP.Net/VB web page.

I created a dataset which connects to an access database.
So I got 2 fields:
- Link where the image is placed.
- Link to go when the image is clicked.

Well, I placed an image in the page using this line:
<img src='images/<%# dsImageBank.FieldValue("image", Container) %>' />

This shows the image of the last loaded record of the dataset.
How to make this random?

I looked at the AdRotator component but it gets the images from XML and
I need to get the images from the database.

Can someone help me out?
I am short on time here.

Thanks,
Miguel
 
H

Hermit Dave

int recordCount = DataSet.Tables[0].Rows.Count;

Random randomInstance = new Random();

int randomnumber = randomInstance.Next(0, recordCount-1);

string imagename = (string) DataSet.Tables[0].Rows[randomnumber]["image"];

If you are using Image class which is a webcontrol then
image1.ImageUrl = "images/" + imagename

that should do the job

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
C

Curt_C [MVP]

pass the list into an array and use the Random() to get a ramdom position in
the array
or get the count and select the index of the DS based on the Random()

Basically there are a bunch of ways, just start playing with the random call
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top