Controls You Can Use on Web Forms ASP.NET Data Source Controls AccessDataSource Control
Basically, to have a working AccessDataSource control on a Web Forms page, you need at least to add the control to the page, point it to the .mdb file, and set the SQL Select statement.
- Declare an <
asp:AccessDataSource
> element on the page, specifying at the very least the following:
- the control’s ID property, which serves as the data source name that data-bound controls use to bind to the data source;
- the DataFile property, which sets the location of the Microsoft Access .mdb file to work with;
- the SelectCommand property, which specifies the SQL query to execute on the data file.
- Optionally set the DataSourceMode property ( defaults to DataSet if not specified ).
<asp:accessdatasource id="ProductType" runat="server"
datafile="~/app_data/gear.mdb"
selectcommand="SELECT DISTINCT Type FROM Products"
datasourcemode="DataReader" />
The following example demonstrates a typical display scenario using AcessDataSource controls with a DropDownList and a GridView control.
Basically, this is how the controls work and interact:
- An AccessDataSource control is bound to the DropDownList control that provides a list of product categories from which the user makes a selection.
<asp:accessdatasource id="ProductType" runat="server"
datafile="~/app_data/gear.mdb"
selectcommand="SELECT DISTINCT Type FROM Products"
datasourcemode="DataReader" />
...
Select Category: <asp:dropdownlist id="filterType" runat="server"
datasourceid="ProductType" datatextfield="Type"
autopostback />
- Another AccessDataSource control is bound to the GridView control, whose SelectCommand property is set to an appropriate SQL query with a given FilterExpression, which is based on the DropDownList control’s SelectedValue.
<asp:accessdatasource id="Products" runat="server"
datafile="~/app_data/gear.mdb"
selectcommand="SELECT Type, ProductId, Brand, Model, Description, Price FROM Products"
filterexpression="Type='{0}’">
<filterparameters>
<asp:controlparameter controlid="filterType"
propertyname="SelectedValue" />
</filterparameters>
</asp:accessdatasource>
- Each time the user makes a selection, the filtered data from the specified datafile is displayed in the GridView control.
<asp:GridView id="ProductsGrid" runat="server"
datasourceid="Products"
... />
Retrieving Data Using the AccessDataSource Control