Making Use of the Repeater Web Server Control - Get the Data
(Page 2 of 5 )
Let's start off by assuming that you've got all of product information stored in a database. This article will not go into creating or populating a database, but it is assumed that you've mastered the basics (by basic, I mean Microsoft Access). I will assume you have the following fields in a 'products' table, in an Access database:
id description manufacturer color price imgPath
This is by no means a normalized database we're dealing with here, nor does it need to be for the sake of teaching how to use a Repeater. Most of the fields are self-descriptive, however the 'imgPath' will be pointing to the relative path of a picture of the product. This is not absolutely necessary, but it's a good idea, and it's also a nice way to show off the flexibility of the Repeater, as you will see.
This article is not a tutorial on data retrieval either. I will though, include the necessary code to do the data extraction from the database, with a small explanation of what's going on. We will need a subroutine called doBinding(), which will do the data retrieval, and the binding.
Sub doBinding()
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Ole DB Services=-4; " & _
"Data Source=C:\Inetpub\wwwroot\store.mdb"
Dim dbConnection As OleDbConnection = New OleDbConnection(strConn)
Dim strSQL As String = "SELECT * FROM products"
Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter( strSQL, strConn)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
We've done the all the needed work to extract the data into a dataset, and all that's left is to tell the repeater to use that data. Do that by 'binding', or attaching, it to the dataset. We'll assume the name of the repeater will be prodRepeater:
prodRepeater.DataSource = dataSet
prodRepeater.DataBind()
End Sub
Now add in the code responsible for calling this subroutine and populating the repeater the first time the page is loaded:
sub page_load( source as Object, e as EventArgs )
if not isPostBack then
doBinding()
end if
end sub
Next: Can You Repeat That Please? >>
More ASP.NET Articles
More By Justin Cook