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
11How can I have my ASP page send HTML-formatted email?62047
147I want to repeat a character or string X number of times, is there a way I can do this without expensive looping operations?49031
200What is the easiest way of extracting certain data from an XML document?54493
77Is there a better way to show debug values from my ASP pages than to Response.Write them in the middle of my page or use a JavaScript alert to pop them up?47057
182What are the differences between ASP.NET Beta 2 and Version 1.0?43770
72When I try to do a Reponse.Redirect, it get an error message about "the HTTP headers are already written..." Why? What can I do?47395
178How can I output the records from my recordset in columns in a table? That is, instead of just one record per row in a table, how can I have multiple records per row?71539
171How can I get all Field Names or Column Names from a recordset or database table ? ..or.. How can I write a general "table dump" for all records and fields in a recordset or table?61000
116I am performing a currency calculation and would like to display the value as 7.30, but it is showing up as 7.3. How can I force two demial places?50969
206How do I delete a file from the Web server's file system?62340


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]