Using the AccessDataSource Control in VS 2005
(Page 1 of 4 )
Microsoft Access Database is file based. Accessing it with an OleDB provider, or an ODBC provider, is the norm. Since the whole database is in one file, a file reference should be all that is necessary to connect to it, and Microsoft has that for you in VS 2005 in the form of the AccessDataSource Control.
However, if the database is protected by a username and password, this is not the method to use, because the AccessDataSource Control can only connect to databases without password protection. The upside in using this control is that it only needs to know where the *.mdb file is located, as we shall see in the course of this tutorial.
AccessDataSource, as the next picture shows, inherits directly from System.Web.UI.WebControls.SQLDataSource, its base class. This code-based tutorial shows how you may use this control to access data on a Microsoft Access Database and then bind the results to two controls, a GridView and a DropDownList.

Creating a web page with the AccessDataSource Control
Create a new web site project named AccessDS as shown in the picture, and onto the default.aspx page in design view, add an AccessDataSource control. This adds a control named AccessDataSource1 as shown. If you click on the arrow at the top right on this control, you get a drop-down showing the tasks that need to be performed. Now click on the hyperlink Configure Data Source....

This should open up the window shown in the next picture, where you will carry out the configuration of the datasource. Either you can enter a relative path to a database *.mdb file, or browse to one on your hard drive.

However, as seen in the next picture, it does not show any source, nor will it let you browse the hard drive.

Here you may have to copy the database file you want to your application folder in the application directory (C:InetpubwwwrootAccessDS App_Data). This will make the nwind.mdb file (in this tutorial) part of the application. Copying to the App_Data directory provides an additional layer of protection, as this directory will not be served.

When you again try to configure the data source you will come to the next screen, where you can now highlight the nwind.mdb file in the right pane and click on the OK button.

Now the configure Data Source window shows the relative path to the database, ~/App_Data/nwind.mdb as shown. Click on the Next button.

Next: Querying the database >>
More Database Code Articles
More By Jayaram Krishnaswamy