Demo of Displaying 10 Random Records

This demo illustrates how to modify the SQL statement to select ten random records. You can refresh this page and you will see ten random FAQs displayed in the DataGrid...


FAQ IDQuestionViews
186I have many duplicate records in my database table. I want to delete all of them except one. How can I do this?50532
165How can I rename a directory on my Web server?41378
91How can I replace each occurrence of a particular pattern in a string with another pattern?48489
113How can I use RecordSet.GetString to create a drop down list? How about other more complex output using GetString? 47156
207How do I copy or move a file from one location to another?70824
24How can I read the contents of a text file through an ASP page?47748
204How can I display XSL-formatted XML data in an ASP.NET Web page?71591
82I let my users put in search strings for any number of fields. How do I convert their inputs into a usable SQL search string?39831
206How do I delete a file from the Web server's file system?49001
200What is the easiest way of extracting certain data from an XML document?41001


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 = "SELECT TOP 10 FAQID, Description, ViewCount FROM tblFAQ ORDER BY NEWID()"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    'Set the datagrid's datasource to the datareader and databind
    myConnection.Open()
    dgRandOrder.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgRandOrder.DataBind()
    myConnection.Close()
  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="FAQID" HeaderText="FAQ ID" />
      <asp:BoundColumn DataField="Description" HeaderText="Question" />
      <asp:BoundColumn DataField="ViewCount" HeaderText="Views" />
    </Columns>
  </asp:DataGrid>


[Return to the article]