Demo of Displaying a Set of Records in Random Order

This demo illustrates how to display a set of records in random order in a DataGrid when you do not have access to change the SQL statement itself. This technique loads the database data into a DataSet, and then adds a new column to the DataTable that stores a random number. Then, a DataView is created and sorted by this random number column. Finally, it is this DataView that is bound to the DataGrid's DataSource. (To see the random display, continually refresh the page.)


Random NumberFAQ IDQuestionViews
34830For session variables to work, must the Web visitor have cookies enabled?121007
1299106How can I find out if a record already exists in a database? If it doesn't, I want to add it.170491
2503161How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?223063
364014Can I send emails without using CDONTS?118071
4253115I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem?220766
494283How can I quickly sort a VBScript array? 215690
5329181How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.917384
6785153How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3?116355
8094118When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... 155075
9698190How do I display data on a web page using arrays instead of Do...While...MoveNext...???...171688


Source Code
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SQLClient" %>
<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    BindData()
  End Sub
	
	
  Sub BindData()
    '1. Create a connection
    Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "sp_Popularity"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    'Set the datagrid's datasource to a DataSet
    Dim myDataAdapter as New SqlDataAdapter(myCommand)    
    Dim myDataSet As New DataSet()
    myDataAdapter.Fill(myDataSet, "FAQs")
    myConnection.Close()
    
    ' Now, randomly order the DataTable's Rows Collection
    Dim myDataTable as DataTable = myDataSet.Tables(0)
    myDataTable.Columns.Add(New DataColumn("RandNum", GetType(Integer)))
    
    Dim i as Integer
    Dim rndNum as New Random()
    For i = 0 to myDataTable.Rows.Count - 1
		myDataTable.Rows(i)("RandNum") = rndNum.Next(10000)
    Next i
    
    Dim myDataView as DataView = myDataTable.DefaultView
    myDataView.Sort = "RandNum"
        
    dgRandOrder.DataSource = myDataView
    dgRandOrder.DataBind()    
  End Sub
</script>

  <asp:DataGrid runat="server" id="dgRandOrder"
         AutoGenerateColumns="False"
         Font-Name="Verdana" Width="85%"
         Font-Size="11pt" HorizontalAlign="Center">
         
    <HeaderStyle BackColor="Navy" ForeColor="White" HorizontalAlign="Center"
                 Font-Size="14pt" Font-Bold="True" />
   
    <Columns>
	  <asp:BoundColumn DataField="RandNum" HeaderText="Random Number" />
      <asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID" />
      <asp:BoundColumn DataField="Description" HeaderText="Question" />
      <asp:BoundColumn DataField="ViewCount" HeaderText="Views" />
    </Columns>
  </asp:DataGrid>


[Return to the article]