asp.net.ph

Skip Navigation Links

Introduction to the AccessDataSource Control

Controls You Can Use on Web Forms   ASP.NET Data Source Controls   AccessDataSource Control


The AccessDataSource control enables you to retrieve and/or modify data from a Microsoft Access database ( .mdb file ) with minimal coding. The data can then be displayed using any HTML or Web server control that supports binding to data, or can be updated using data-bound controls that support in-place editing, such as the GridView, FormView, and DetailsView controls.

The AccessDataSource control inherits most of its properties from the SqlDataSource class, and includes a DataFile property that simpifies connecting to a Microsoft Access database.

Connecting to a Database using the AccessDataSource Control

Basically, to connect to a Microsoft Access database using the AccessDataSource control, all you need to do is set the location of the Access .mdb file, using the DataFile property. The AccessDataSource takes care of the underlying connection to the database.

The DataFile property expects a universal naming convention ( UNC ) directory path that points to an Access database file. For more information on acceptable path values, see DataFile in the class library.

While the AccessDataSource supports both the ConnectionString and ProviderName properties, note that these are meant only for obtaining the values of said properties. You need not set the ConnectionString or the ProviderName properties of the control. In fact, attempting to set either property raises an exception.

The AccessDataSource control is configured to use the System.Data.OleDb .NET Data Provider, which in turn uses the Microsoft.ACE.OLEDB.12.0 provider to connect to Access databases. The connection string is automatically generated using this default provider, along with the given DataFile value.

NOTE: Because the ConnectionString property of the control is set internally and can not be modified, the AccessDataSource class does not support connecting to Access databases that are protected by a user name or password. To retrieve data from a password-protected Access database, use the SqlDataSource control instead.

The following example demonstrates how you can use a root-relative path ( "~/directory/filename.mdb" ) to connect to an Access database that is located in the App_Data folder of the current Web application.

<asp:accessdatasource id="plans" runat="server"
   datafile="~/app_data/plans.mdb"
   selectcommand="SELECT Design, Model, Description, PlanId FROM Plans 
      WHERE Type in ( 'English', 'French', 'Tudor', 'Victorian' )" />
Run Sample | View Source

IMPORTANT: For security, Access databases should be located in the App_Data directory of the Web application, as this location is internally configured to reject direct HTTP requests to the data files from client browsers. The data files can be accessed only by applications running on the same domain.

See Also

Adding AccessDataSource Controls to a Web Forms Page



© 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