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
161How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?238089
33I've seen function definitions that include ByRef and ByVal statements. What are these?52331
138I'm getting an error (80020009) when trying to work with a value from a Memo (or text or blob) column. For example, I get this error when I do: Response.Write objRS(MemoColumnName)54771
41How can I delete an existing cookie?82522
165How can I rename a directory on my Web server?54063
192When using the FSO, my ASP page tends to "hang", but doesn't report any errors.59461
213In ASP.NET, how can I create an XmlDocument instance from an XML string? Similarly, given an XML string, how can I display the data in a data Web control?73586
69When I display the result from a TEXTAREA in a Web page, the line breaks are removed. How can I maintain a TEXTAREAs line breaks in an Web page?65826
113How can I use RecordSet.GetString to create a drop down list? How about other more complex output using GetString? 59771
60How can I retrieve emails from a service like Yahoo! Mail?55581


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]