March 8, 2021
Hot Topics:

Create Quick Database Interfaces with ASP.NET Dynamic Data

  • By Jani Järvinen
  • Send Email »
  • More Articles »


Have you ever had the need to add database editing support to your ASP.NET web application? Most likely, you have. As you know, this work is not particularly difficult, but it is tedious and often repetitive work. On the lowest level, you analyze your database tables, and then create suitable editing controls for each table field, write logic to update the database, and you are done. Next, repeat the same for every table you need to support.

If you feel you could spend your time better, then Microsoft has a possible time-saver made just for you. Called ASP.NET Dynamic Data and part of .NET 3.5 SP1 (Service Pack 1), this new technique lets you quickly create web-based interfaces for your database editing needs. ASP.NET Dynamic Data is written so that it supports all the basic database operations: reading, creating, updating and deleting. Shortly put, the web interfaces supports the basic CRUD operations for those tables you want to enable.

Figure 1 shows an example of an editing view created by ASP.NET Dynamic Data. The idea is that by default, the project template contains a page for each of the four basic database operations, and you will then specify which tables you wish to enable for viewing and editing.

Click here for larger image

Figure 1. An example editing page generated by Dynamic Data.

This article will walk you through creating and customizing an ASP.NET Dynamic Data web application, starting from the initial steps to a complete application. Code examples are written using C#, and the familiar SQL Server sample database Northwind is used as the data source.

Getting started with Dynamic Data

Developing your first ASP.NET Dynamic Data application is simple, but still the results are effective. To get started, you first need to install both Visual Studio 2008 Service Pack 1 and .NET 3.5 Service Pack 1. After installing both, your Visual Studio's New Project dialog box will have two ASP.NET Dynamic Data templates to choose from (Figure 2).

Click here for larger image

Figure 2. Visual Studio 2008 SP1 brings the new Dynamic Data project templates.

Since ASP.NET Dynamic Data is aimed at writing user interfaces for databases, it's only natural to have different options for database access. The two available options are using LINQ to SQL to access your databases, and the other is to access them using ADO.NET Entity Framework (ADO.NET Entities for short). Although it's technically possible to mix these two data access options in a single ASP.NET Dynamic Data project, the idea is that you select one, and then stick to that.

That said, you often have to make an early choice of your data access strategy when starting development of ASP.NET Dynamic Data applications. Given this, it is wise to give some thought as to how you would like to access your databases before starting your project.

For this article, the ADO.NET Entity Framework is chosen, as it provides an elegant solution to many database access needs. Thus, the correct template icon to select from the New Project dialog box would be named "ASP.NET Dynamic Data Entities Web Application". Once you name your project and click OK, Visual Studio will create a new solution for you with a skeleton application in place.

Although many other application types can be run successfully after creating the project, ASP.NET Dynamic Data applications make an exception. If you try to directly run the application without changing anything, you will run into a quite disappointing error saying that "there are no accessible tables". But be brave, this problem is easy to fix!

The fix you need to do is easy to understand, if you recall that you haven't yet specified any database for the application. Without a database connection, the application wouldn't be able to display any data. (Another thing is whether the application could give you a clearer error message.) To solve the problem is then, a matter of defining a database connection.

Since the project template you chose earlier was designed for ADO.NET Entities, you should next add such a connection to your project. To do so, right-click the main project node in Visual Studio's Solution Explorer, and then choose the Add/New Item command from the popup menu. This will bring the Add New Item dialog box onto the screen. From this dialog box, select the ADO.NET Entity Data Model object (this is easiest to find from the Data section on the left), and then enter a name for the object. It's a good idea to name the object after your database, for instance NorthwindModel for the SQL Server sample database.

Once you click OK, the Entity Data Model Wizard will open (Figure 3). You can either create a model from a database, or an empty model. Since you will need a SQL database for ASP.NET Dynamic Data applications in any case, it is usually the easiest to generate the model after an existing database. The next step in the wizard is to specify the connection string for your database. This of course depends on your database, but at minimum you will need the server's (host) name, your database name, and finally a user id and a password.

Click here for larger image

Figure 3. The Entity Data Model Wizard.

After you have specified all the necessary details, continue to the next step in the wizard. Here, you would specify all the tables, views and stored procedures you want to include in your model. The easiest, but not always the best option, is to work directly with the underlying SQL tables. For instance, using views would let you later change table structure without affecting the application. Once you have selected your database objects (from now on, this article will refer to these collectively as tables for simplicity), you can click the Finish button in the wizard, and then let Visual Studio create the entity model for you. The results should appear similar to those in Figure 4. In your project, you should have a new file: a .edmx file which describes your model in XML.

Click here for larger image

Figure 4. The Entity Data Model showing in Visual Studio./BODY>

Page 1 of 4

This article was originally published on June 8, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date