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
188How do I use regular expressions from an ASP.NET Web page?70022
162What are the differences between ASP.NET Beta 1 and Beta 2?52117
74This question comes from Steve: How can I kill one session variable at a time? "I don't want to have to abandon the whole session, just kill off a few session variables that were used?"65649
27How do I get SQL to accept an apostrophe in various queries, such as when I try to add a user named "O'Brien"?79199
19How can I return the current date/time?58677
172How can I display images that reside in my database through an ASP page?110298
127How can I count the number of substrings in a string?63085
53How can I change a string like $51,234.98 into a number (51234.98) 64982
190How do I display data on a web page using arrays instead of Do...While...MoveNext...???...186006
29How can I determine if a visitor has cookies support enabled in his/her browser?94329


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]