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
128How can I convert binary data into a string? 76971
169Fundamentals of client-server ASP:   How can my ASP code call a JavaScript (or VBScript) function in the browser? How can I use a function or event in the browser to call a VBScript function in ASP? 61858
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?52361
97How can I get a count of the number of dimensions in an array?58949
180How can I install Personal Web Server (PWS) onto Windows XP Home edition?116884
87Q: How can I round or slide a number to the nearest integer multiple of my choice? (For example rounding 13.3, 22.75 and 6 to the nearest 5 = 15, 25, 5 respectively)53456
139Why does the .RecordCount property return a value of -1?63999
212How can I create an XML document from an ASP.NET Web page?39878
52How can I display all of the cookies/cookie values for a user through a Web page?59803
124How can I perform a Reverse DSN Lookup through ASP?50596


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]