Jaa


EntityDataSource Quickstart Example

The example in this topic demonstrates use of the ADO.NET EntityDataSource control to implement a data binding scenario. The example application displays SalesOrderDetail items associated with a SalesOrderHeader identified by the identity key of the SalesOrderHeader. The data model used in the application is based on the AdventureWorks sample database that ships with SQL Server 2005.

The following screen display shows the tool in use. A list box on the left side of the page contains identity keys of SalesOrderHeader items. In the display, SalesOrderHeader number 43666 has been clicked. The associated SalesOrderDetail items are displayed by the Gridview control on the right.

EntityDataSource Quickstart Example

The example is implemented in the following stages:

  • Create an ASP.NET Web application.

  • Add an ADO.NET Entity Data Model based on the AdventureWorks database.

  • Add a list box to display SalesOrderHeader keys.

  • Add a GridView control to display associated SalesOrderDetail items.

  • Add and configure two EntityDataSource controls to supply data to the list box and GridView controls.

  • Add an event handler for the list box selected-index changed event.

Procedures

Creating the ASP.NET Web Application

This procedure creates the ASP.NET application.

To create the ASP.NET Web application:

  1. From the File menu in Visual Studio, create an ASP.NET application. Name the project GetOrders EDSC. The Default.aspx page mark-up opens showing empty HTML form and div syntax.

  2. Give the page a title between the title mark-up tags.

Defining the AdventureWorks Sales Model

This procedure defines the AdventureWorks Sales model. For more information, see AdventureWorks Sales Model (EDM).

To create the AdventureWorks Sales Model:

  1. Right-click the project GetOrders EDSC in the Solution Explorer.

  2. Click Add/New Item.

  3. Select ADO.NET Entity Data Model and name the new data model AdvWksSales.edmx.

  4. When the Entity Data Model Wizard appears, select Generate from Database and click Next.

  5. Create a connection to the AdventureWorks database, and click Next.

  6. When the Choose Database Objects dialog box appears, select Address, Contact, Product, SalesOrderDetail, and SalesOrderHeading. Click Finish.

  7. The AdvWksSales.edmx designer opens and displays the entities and associations of the data model. Note the one-to-many association between the SalesOrderHeader entity and the SalesOrderDetail entity. These two data types and the association between them will be used in the sample application.

  8. Build the project to create the data model.

Adding Controls to Display Data

This procedure adds the two data controls that are needed to display the SalesOrderHeader keys and associated SalesOrderDetail items.

To add display controls to the page:

  1. Open the Default.aspx design view in Visual Studio and drag the div boarder down so that there is enough room to add controls to the page.

  2. Add a label with text: Select SalesOrderHeaderID.

  3. Below the label add a list box. The default ID, ListBox1, can be used. Set the size and position of the list box according to the page mark-up at the bottom of this page.

  4. Add a GridView control to the page and set the position according to the page mark-up at the bottom of this topic. The default ID GridView1 can be used.

  5. Add a label above the GridView control with text: SalesOrderHeader ID.

  6. Add another label above the GridView. This label will be used in a parameter of a CommandText. Give it an ID that reflects its purpose, such as: labelHeaderIDFromList.

  7. Set the text of this label to an empty string.

Adding and Configuring EntityDataSource Controls

These procedures create the EntityDataSource controls and bind the display controls to these data source controls.

To add the EntityDataSource controls:

  1. Add an EntityDataSource control to the page. Name this EntityDataSource control EntDataSrc_OrderID. This control will be bound to ListBox1 to display SalesOrderHeader keys.

  2. Add another EntityDataSource control to the page. Name this EntityDataSource control EntDataSrc_OrderDetails. This control will be bound to the Gridview1 to display SalesOrderDetail entities associated with the SalesOrderHeader identity key selected by the user in ListBox1.

    Note

    If the project has not been built since adding the Entity Data Model (EDM) for AdvWksSalesModel, build the project now, or the following steps will not work as described. Building the EDM project places the EDM metadata artifacts in the location designated by the connection string in the WebConfig file.

To binding the list box control to the SalesOrderHeader data source:

  1. Click the > symbol on the EntityDataSource control.

  2. Click Configure Data Source.

  3. When the Configure Object Context dialog box appears, select Named Connection AdventureWorksEntities from the drop-down list. This identifies a connection string from the WebConfig file.

  4. From the DefaultContainerName drop-down list, select AdventureWorksEntities. This is the name of the EntityContainer used by the data model in its design schema. Click Next.

  5. When the Configure Data Selection dialog box appears, select SalesOrderHeader from the EntitySetName drop-down list. This type is not part of an inheritance hierarchy, so the EntityTypeFilter can be left blank.

  6. From the Select Statement check boxes, check SalesOrderID. Click Finish.

  7. Click the > symbol on ListBox1, and select Choose Data Source.

  8. When the Choose a Data Source dialog box appears, choose EntDataSrc_OrderID from the Select a Data Source drop-down list. Select SalesOrderID in the next two drop down lists.

  9. Click OK.

    Note

    At this stage, it is instructive to run the application. The list box displays all the identity keys of SalesOrderHeader entities in the data source. This information is useful when the rest of the application is implemented and the user can select one key and display all the SalesOrderDetail entities associated with the SalesOrderHeader in Gridview1 on the right side of the page.

To bind the grid view control to the SalesOrderDetail data source:

  1. Select the EntityDataSource control named EntDataSrc_OrderDetails on the design surface of the Default.aspx page.

  2. Click the > symbol on the EntityDataSource control.

  3. Click Configure Data Source.

  4. When the Configure Object Context dialog box appears, select Named Connection AdventureWorksEntities from the drop-down list.

  5. From the DefaultContainerName drop-down list, select AdventureWorksEntities.

  6. Click Next.

  7. When the Configure Data Selection dialog box appears, select SalesOrderDetail from the EntitySetName drop-down list. This type is not part of an inheritance hierarchy, so the EntityTypeFilter can be left blank.

  8. From the Select Statement check boxes, check SalesOrderDetailID, OrderQty, ProductID, UnitPrice, and ModifiedDate.

  9. Click Finish.

  10. Right-click the EntityDataSource control and select Properties.

  11. From the Properties list select Where, and click the ellipses at the right side of the text box.

  12. When the Expression Editor dialog box appears, type the following text into the Where Expression text area: it.[SalesOrderID] = @parSalesOrderID.

  13. Click the Add Parameter button under the Parameters list box. Give the new parameter the name parSalesOrderID. This parameter will be obtained from the text of a label that is set when the user selects an identity key in the SalesOrderHeader list box. This is specified in the Parameter Source drop-down list.

  14. Select Control.

  15. From the ControlID drop-down list, select labelHeaderIDFromList.

  16. Click OK.

Defining the Selected-Index Change Event Handler for the List Box

This final procedure connects the user's selection of an identity key for a SalesOrderHeader to a query that returns properties of all SalesOrderDetail entities associated with the selected SalesOrderHeader.

To create an event handler for the SelectedIndexChanged event of ListBox1:

  1. Select ListBox1 in the Web page design surface.

  2. Right-click ListBox1, and select Properties.

  3. In the Properties list, click the events option.

  4. Double-click the SelectedIndexChanged event. This opens the ASP.NET code-behind page, Default.aspx.cs or Default.aspx.vb, and shows the code block for the event handler.

  5. Add the following code to the event handler:

        labelHeaderIDFromList.Text = ListBox1.SelectedValue;
        GridView1.DataSource = EntDataSrc_OrderDetails;
        GridView1.DataBind();
    
  6. Set the AutoPostBack property of ListBox1 to true, so the server can read the selection as soon as it is made.

    When the SelectedIndexChanged event fires, the predefined query with the parameterized Where clause runs. The properties displayed in GridView1 are those of SalesOrderDetail entities associated with the SalesOrderHeader selected in ListBox1.

See Also

Concepts

EntityDataSource Designer

Other Resources

Data Selection using EntityDataSource

Change History

Date

History

Reason

July 2008

Added topic.

SP1 feature change.