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
93How can I quickly determine if a string exists within an array?73272
173How can I pick a random record from a table, using a stored procedure?66022
57Okay, if I can't do a conditional #include then how can I conditionally include things in an ASP page?44193
58Why do I get a type mismatch error from my query? Why do I get the message Syntax error (missing operator) from my query? Why do I get a Too few parameters expected 1 error from my query? 61107
85How can I disable the back button?46537
17How can I find the position of a certain character (or substring) within a string?65717
59(1) How can I have two dates and select all records that have a date field with a value between those two dates? (2) How can I select all records in a given month?63400
114How can I shuffle a deck of cards or similar set of sequential numbers? How can I reorder a list of things in random order? 46259
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
49Can I send my output to a different window or frame than the one that asked for the current ASP page?48632

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)
  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
    dgRandOrder.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
  End Sub

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

[Return to the article]