Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi sir , I am Using This Code To show Random Images .. But When I run this code it is not giving me Random Records .. but When I removed ORDER BY ImageID() it is working good
Please Anybody Tell me How to Show Random Records ?

I am Fresher sir ,Thanks In Advance :)

My back end is SQL SERVER
and My Code Is :

C#
protected void BindDataList()
{
        con.Open();

        //Query to get ImagesName and Description from database
        SqlCommand command = new SqlCommand("SELECT TOP 5 * FROM [SlideShowTable1]  where (startddate <= @sdate) AND (endddate >=@edate) AND AddType=@Add ORDER BY ImageID() ", con);

        command.Parameters.AddWithValue("@sdate", System.DateTime.Now.ToString("M/d/yyyy"));
        command.Parameters.AddWithValue("@edate", System.DateTime.Now.ToString("M/d/yyyy"));
        command.Parameters.AddWithValue("@Add", "Right_Side_Add");
      
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet dt = new DataSet();
        da.Fill(dt);

        dllImages.DataSource = dt;
        dllImages.DataBind();
        con.Close();
  
}
Posted
Updated 26-Aug-13 0:51am
v3

Try this

C#
SqlCommand command = new SqlCommand("SELECT TOP 5 * FROM [SlideShowTable1] where (startddate <= @sdate) AND (endddate >=@edate) AND AddType=@Add ORDER BY newid() ", con);
 
Share this answer
 
Comments
Amirsalgar1 26-Aug-13 7:03am    
thanx happy :)
pradiprenushe 26-Aug-13 7:09am    
welcome
You can try using RAND()[^] function of SQL Server. See the similar solution:
How to request a random row in SQL?[^]
Selecting Rows Randomly from a Large Table[^]

--Amit
 
Share this answer
 
Comments
Amirsalgar1 26-Aug-13 7:03am    
thanx happy :)
_Amy 26-Aug-13 7:07am    
Welcome. :)

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