Simple Demo of Displaying the Authors from the authors Table

This demo illustrates how to have the contents of the authors table displayed in a DataGrid. Note that the DataGrid is setup for sorting (that is, the columns have their SortExpression property set, and the DataGrid has specified an onSortCommand event handler), but that currently, clicking the sorting headers does nothing.


First NameLast NameCityStateZip
AbrahamBennetBerkeleyCA94705
AkikoYokomotoWalnut CreekCA94595
AlbertRingerSalt Lake CityUT84152
AnnDullPalo AltoCA94301
AnneRingerSalt Lake CityUT84152
BurtGringlesbyCoveloCA95428
CharleneLocksleySan FranciscoCA94130
CherylCarsonBerkeleyCA94705
DeanStraightOaklandCA94609
DirkStringerOaklandCA94609
HeatherMcBaddenVacavilleCA95688
Innesdel CastilloAnn ArborMI48105
JohnsonWhiteMenlo ParkCA94025
LiviaKarsenOaklandCA94609
MarjorieGreenOaklandCA94618
MeanderSmithLawrenceKS66044
MichaelO'LearySan JoseCA95128
MichelDeFranceGaryIN46403
MorningstarGreeneNashvilleTN37215
ReginaldBlotchet-HallsCorvallisOR97330
SherylHunterPalo AltoCA94301
StearnsMacFeatherOaklandCA94612
SylviaPanteleyRockvilleMD20853


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)
  ' Only bind the data on the first visit to the page
  If Not Page.IsPostBack
    GetAuthors("au_fname asc")
  End If
End Sub

Sub GetAuthors(sSortStr as string)
	Dim cn as SQLConnection
	Dim cmd as SQLCommand
	Dim rdr as SQLDataReader
	Dim sConnectString as String = ConfigurationSettings.AppSettings("connectionStringPubs")
	Dim sSql as String = "SELECT au_fname, au_lname, city, state, zip from authors order by " & sSortStr
	

	' Connect to the database
	cn = New SQLConnection(sConnectString)
        cn.open()

	' execute the SQL 
	cmd = New SQLCommand(sSQL, cn)

	' 
	rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

	' Associate the data grid with the data
	DispAuthors.DataSource = rdr
	DispAuthors.DataBind()
End Sub

' this method is called when user clicks on any of the column headings
Sub SortAuthors(sender As Object, e As DataGridSortCommandEventArgs)

End Sub
</script>

<form runat="server">
     <ASP:Datagrid id="DispAuthors" runat="server" AutoGenerateColumns="False" 
				         AllowSorting="true" 
                         onSortCommand="SortAuthors">
    	<AlternatingItemStyle BackColor="Silver"></AlternatingItemStyle>
	<ItemStyle Font-Size="Smaller" Font-Names="helvatica" VerticalAlign="Top"></ItemStyle>
	<HeaderStyle Font-Size="Medium" Font-Names="Arial" Font-Bold="True" ForeColor="Teal"></HeaderStyle>
	<Columns>
 	      <asp:BoundColumn DataField="au_fname" SortExpression="au_fname ASC" HeaderText="First Name" />
	      <asp:BoundColumn DataField="au_lname" SortExpression="au_lname" HeaderText="Last Name" />
	      <asp:BoundColumn DataField="city" HeaderText="City" />
	      <asp:BoundColumn DataField="state" SortExpression="state" HeaderText="State" />
	       <asp:BoundColumn DataField="zip" HeaderText="Zip" />
	</Columns>
   </asp:datagrid>
</form>


[Return to the article]