asp.net.ph

Skip Navigation Links

Adding AccessDataSource Controls to a Web Forms Page

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.

To add an AccessDataSource control to a Web Forms page

  1. Declare an <asp:AccessDataSource> element on the page, specifying at the very least the following:
    1. the control’s ID property, which serves as the data source name that data-bound controls use to bind to the data source;
    2. the DataFile property, which sets the location of the Microsoft Access .mdb file to work with;
    3. the SelectCommand property, which specifies the SQL query to execute on the data file.
  2. 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:

  1. 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 />
  2. 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>
  3. 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"
          ... />

Using the AccessDataSource Control
Run Sample | View Source

See Also

Retrieving Data Using the AccessDataSource Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note