Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow,

I'm implemeting a web site on asp.net 4.5, i have a table database products with columns (id,name,size,price,date,quantity) where i have imported some items.

My question is how can i preview in my home.aspx(6 random items every time (different or change automatically randomly) my home.aspx).

Thnx in advance for your time!
Posted
Comments
Richard C Bishop 23-Sep-13 15:08pm    
You can iterate through the data using a loop and call the rows by index after you load your results into a datatable. Then use a random number generator with a max value that is the length of your datasource.

I would suggest doing the random selection of products in your DB query not in asp.net. If you are using SQL Server for the DB, then something like this will help:
SQL
SELECT TOP 6 *
  FROM YourTable
  ORDER BY NEWID()

The NewID does the random sample
See this link: http://msdn.microsoft.com/en-us/library/cc441928.aspx[^]

Also, Google "tsql random sample" for more help

That was an interesting question, good luck!
 
Share this answer
 
Comments
Member 10186638 24-Sep-13 1:31am    
mgoad99 gives nice solution and its work for me when I was creating web application for Online Test
JasonTsoum77 24-Sep-13 9:50am    
Thank you for your help
Jason,
Assuming that you know how many entry's there are in the DB and
that the entry's have ID's
If you use
VB
Dim r As New Random
       r.Next(0, 100)

In VB
Or

C#
Random r = new Random();
   r.Next(0,100);

in C# then you can pick a random ID from your DB.

I think you can figure out how to use Random in some
code to take six.

Grtz
Tom
 
Share this answer
 
Comments
JasonTsoum77 24-Sep-13 9:50am    
Thank you for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900