This example will walk you through creating a user control which will display data from MS Access database in a grid.
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.
- Create MS Access database and name it RadGridAccessDataSource.mdb
- Create a new table in this database and name it Languages
- Add the following three fields to the Languages table
- ID – Autonumber
- Language – Text (field size 255)
- Country – Text (field size 255)
- Populate a few fields. Here is an example:
- Language = "English", Country = "USA"
- Language = "French", Country = "France"
- Language = "Italian", Country = "Italy"
- Save the database file to the App_Data folder of your Web site
- Create a new folder in the root of your Web site; name it AccessControls
- Create a new User Control in that folder and name it RadGridAccessDatasource.ascx
- 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.
- Save the control and upload it to Sitefinity toolbox.
- 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.
 |
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