Sitefinity CMS

MS Access Data Source Send comments on this topic.
See Also
Developing with Sitefinity > Data Access > Non-provider Data Access > MS Access Data Source

Glossary Item Box

This example will walk you through creating a user control which will display data from MS Access database in a grid.

 

Download the Sample Code from here.

 

As a native ASP.NET 2 application, Sitefinity can work with standard AccessDataSource control. While MS Access is not a preferred way of working with data for Web sites due to its performance limitations, it is not unusual to use MS Access as data source for small projects. A common scenario is when data created by one of the business users (with MS Office Access) needs to be displayed on the Web site for a brief period of time, and creating a full-blown implementation does not make economical sense.

 

Following are the steps to create a user control which will display the data from MS Access database. This user control, naturally, can be uploaded to Sitefinity toolbox as any other control.

  1. Create MS Access database and name it RadGridAccessDataSource.mdb
  2. Create a new table in this database and name it Languages
  3. Add the following three fields to the Languages table
  4. IDAutonumber
  5. LanguageText (field size 255)
  6. CountryText (field size 255)
  7. Populate a few fields. Here is an example:
  8. Language = "English", Country = "USA"
  9. Language = "French", Country = "France"
  10. Language = "Italian", Country = "Italy"
  11. Save the database file to the App_Data folder of your Web site
  12. Create a new folder in the root of your Web site; name it AccessControls
  13. Create a new User Control in that folder and name it RadGridAccessDatasource.ascx
  14. Copy and paste the following code in your control (in the control, not in the code behind)

    RadGridAccessDatasource.ascx Copy Code
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="AccessDataSource1" GridLines="None"
       
    AutoGenerateColumns="False">
       
    <MasterTableView DataSourceID="AccessDataSource1" DataKeyNames="ID">
           
    <Columns>
               
    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" SortExpression="ID" UniqueName="ID">
               
    </telerik:GridBoundColumn>
               
    <telerik:GridBoundColumn DataField="Language" HeaderText="Language" SortExpression="Language" UniqueName="Language">
               
    </telerik:GridBoundColumn>
               
    <telerik:GridBoundColumn DataField="Country" HeaderText="Country" SortExpression="Country" UniqueName="Country">
               
    </telerik:GridBoundColumn>
           
    </Columns>
       
    </MasterTableView>
    </
    telerik:RadGrid>
    <
    asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/RadGridAccessDataSource.mdb"
       
    SelectCommand="SELECT * FROM [Languages]"></asp:AccessDataSource>

    As you can see, the control is very simple. We have declared RadGrid control, defined its columns (based on the fields in our Languages table) and bound it to AccessDataSource control. Obviously, you do not need to code this, since it can all be done visually in Visual Studio design view. You could drag the AccessDataSource control from the toolbox, select the DataFile and specify the SelectCommand. Then, you need to drag the RadGrid control, bind it to the AccessDataSource and use built-in Grid designer to define the columns.

  15. Save the control and upload it to Sitefinity toolbox.
  16. Finally, in Sitefinity paged editor - drag the newly-uploaded control onto the page, publish the page and you are done. The data from MS Access database will be displayed on your page.
  17. You can use AccessDataSource to perform all CRUD (create-read-update-delete) operations, but for the sake of simplicity this sample demonstrates only how to read data. Bear in mind that due to performance limitations MS Access is not the best choice of a database for production Web sites (unless you are working on a very small project and not expecting large traffic).

     

    See Also