Displaying Random Data

This live demo illustrates how to display five random records from the Articles table.


TitleDate WrittenCommentsViews
Displaying Records in a DataGrid in Random Order5/10/2003This article, by Scott Mitchell, examines how to display the results of a SQL query in random order in a DataGrid.6030
Deciding When to Use the DataGrid, DataList or Repeater7/23/2003Learn about ASP.NET's three controls for displaying data: the DataGrid, the DataList, and the Repeater. Each of these controls has unique traits and associated advantages and disadvantages. When creating an ASP.NET application that displays data, it is important to choose the right control for the job. As we will see in this article, choosing whether to use the DataGrid, DataList, or Repeater is a tradeoff between three factors: usability, development time, and performance.6878
Creating a Custom DataGridColumn Class10/2/2002This article, by John Dyer, looks at how to create a custom DataGrid column control. Specifically, John's article examines creating a LimitColumn - a DataGrid column that can limit the number of textual characters that appears.4411
Understanding the Differences Among the DataGrid, DataList, and Repeater5/21/2003This article, by Scott Mitchell, examines the similarities and differences among the DataGrid, DataList, and Repeater Web controls. (For those who own a copy of ASP.NET Data Web Controls Kick Start - this material follows along closely with the material presented in Chapter 1, "What Are Data Web Controls?")15236
Creating a Fully Editable DataGrid4/18/2003This article, by Joel Gray, shows how to create a DataGrid that is completely editable - that is, each row can be edited instead of only one row being editable at a time.23990


Source Code

<%@ Page Language="VB" %>
<script runat="server">

    Sub Page_Load(sender as Object, e as EventArgs)
      If Not Page.IsPostBack then
        dgArticles.DataSource = GetArticles()
        dgArticles.DataBind()
      End If
    End Sub
    
    
     Function GetArticles() As System.Data.SqlClient.SqlDataReader
         Dim connectionString As String = "connection string"
         Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)
    
         Dim queryString As String = "SELECT TOP 5 ArticleID, [Articles].[Title], [Articles].[URL], [Articles].[dateAuthored], [Articles"& _
"].[Comments], [Articles].[ClickThroughs] FROM [Articles] ORDER BY NEWID()"
         Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
         sqlConnection.Open
         Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
         Return dataReader
     End Function    
    
         Sub dgArticles_Sort(sender as Object, e As DataGridSortCommandEventArgs)
           dgArticles.DataSource = GetArticles(e.SortExpression)
           dgArticles.DataBind()
         End Sub

</script>

    <form runat="server">
        <p>
            <asp:DataGrid id="dgArticles" runat="server" AllowSorting="True" AutoGenerateColumns="False" Font-Size="10pt" Font-Names="Verdana" OnSortCommand="dgArticles_Sort">
                <HeaderStyle font-size="13pt" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="#006699"></HeaderStyle>
                <AlternatingItemStyle backcolor="#EEEEEE"></AlternatingItemStyle>
                <Columns>
                    <asp:HyperLinkColumn DataNavigateUrlField="ArticleID" DataNavigateUrlFormatString="/Articles/Goto.aspx?ID={0}" DataTextField="Title" SortExpression="Title" HeaderText="Title"></asp:HyperLinkColumn>
                    <asp:BoundColumn DataField="DateAuthored" SortExpression="dateAuthored DESC" HeaderText="Date Written" DataFormatString="{0:d}"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Comments" HeaderText="Comments"></asp:BoundColumn>
                    <asp:BoundColumn DataField="ClickThroughs" SortExpression="ClickThroughs DESC" HeaderText="Views" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>            
        </p>
    </form>
    

[Return to the FAQ...]