ASP.net 2.0 contains the AccessDataSource control that is used to extract data from an Access database to the ASP.net 2.0 (. aspx) page. The properties owned by this control are simple. The most important attribute of AccessDataSource is the DataFile property, which points to the path of the MDB file on the hard disk. Other attributes owned by AccessDataSource also have SelectCommand, which is used to set a statement that displays the result set (tables and columns) that need to be returned. SelectCommand must be defined by using SQL syntax.
In vWD, you can add AccessDataSource controls to a page in two ways. If the MDB file is added to Database Explorer as in the previous example, you can drag and drop the column name to the page, and VWD will automatically create the AccessDataSource control and the GridView to display the data. If you do not want to use the GridView or want to customize the control, you can simply add the AccessDataSource control from the tool box and install it through the Configuration Wizard. The wizard will prompt you to browse for the data file and allow you to specify SelectCommand by selecting the column in the table in the database. If you are entering a page outside of VWD, you can enter the following code as follows:
<asp:AccessDataSource>
ID="MySourcName"
Runat="server"
DataFile="MyMDBName.mdb"
SelectCommand="SELECT MyField1, MyField2 FROM MyTable">
</asp:AccessDataSource>
The features that need to be mentioned again are simple. All you have to offer is an ID, an MDB filename, and a SelectCommand.
This article is about the content of the data source control, but recall that the data source control does not render anything visible on the page. So in order to give you a visual way to test the data source control, the next "Try" describes the basic steps of using the GridView control (evolved from the ASP.net 1 version of the DataGrid control) to display data in a table. Note that the focus of this chapter is not on the configuration and customization of the GridView, in which case the emphasis is on the data source control. In the 5th chapter that follows, the GridView control will be studied in more detail.
Try #2--connect to MDB and display data in GridView
In this case, the Northwind on the page displays information about the products that have been sold.
(1) Launch Page Editor and browse to C:\Websites\BegAspNet2Db\ch02\.
(2) Add a page named Tio-ch02-2-displayaccessdata from the Web Form template. Click the tab below to switch to Design view.
(3) at the top of the page, enter a title, such as "Connect to an MDB data source demo," and put the same text in the title of the page. Although this step is not discussed in every exercise, we recommend that you include a title and HTML text on each page so that you do not find the page you are browsing.
<title>TIO ch02-1 Display MDB Data</title>
<body>
<body>
(4) Display the tool box (Menu:view|toolbox or type ctrl+alt+x) and expand the Data panel of the tool box.
(5) Drag a AccessDataSource control to the page.
(6) The Convenience task pane for the control should appear automatically, and if the Panel does not appear automatically, you can select a new data source and click the small arrow on the top right to open the convenience task panel. Click Configure Data Source.
(7) in the Choose a database step, click the Browse button and select C:\Websites\BegAspNet 2db\app_data\northwind.mdb, and note that the designer converts the file description to a relative reference, that is, the symbol (~ ) represents the root directory of the site.
(8) In the Configure Select Statement dialog box, select the Specify Columns option and select the Products table. Click the asterisk to select all columns. Click Next, select Query, and finish.
(9) Select the data control and display the Properties window by pressing F4. Change its ID to northwindproducts.
(10) Return to the data panel of the Toolbox, double-click on the GridView to add the control to the page and enter Common Tasks Menu, and then click Choose Data Source. Select Northwindproducts and end the wizard.
(11) Select the GridView and change its ID to northwindproducts. The page should look like the following:
<%@ page language="VB" %>
<body>
<form runat="server">
<asp:accessdatasource id="NorthwindProductsAccDataSource" runat="server"
selectcommand="Select * From Products"
datafile="~/App_Data/Northwind.mdb">
</asp:accessdatasource>
<asp:gridview id="NorthwindProductsGridView" runat="server"
datasourceid="NorthwindProductsAccDataSource">
</asp:gridview>
</form></body>
(12) Open the browser and view the C:\Websites\BegAspNet2Db\ch02\ch02_TIO_2_ dis-playaccessdata.aspx (see Figure 2-2).
Figure 2-2