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.
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.
Creating the ASP.NET Web Application
This procedure creates the ASP.NET application.
To create the ASP.NET Web application:
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.
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:
Right-click the project GetOrders EDSC in the Solution Explorer.
Click Add/New Item.
Select ADO.NET Entity Data Model and name the new data model AdvWksSales.edmx.
When the Entity Data Model Wizard appears, select Generate from Database and click Next.
Create a connection to the AdventureWorks database, and click Next.
When the Choose Database Objects dialog box appears, select Address, Contact, Product, SalesOrderDetail, and SalesOrderHeading. Click Finish.
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.
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:
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.
Add a label with text: Select SalesOrderHeaderID.
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.
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.
Add a label above the GridView control with text: SalesOrderHeader ID.
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.
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:
Add an EntityDataSource control to the page. Name this EntityDataSource control EntDataSrc_OrderID. This control will be bound to ListBox1 to display SalesOrderHeader keys.
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.
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:
Click the > symbol on the EntityDataSource control.
Click Configure Data Source.
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.
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.
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.
From the Select Statement check boxes, check SalesOrderID. Click Finish.
Click the > symbol on ListBox1, and select Choose Data Source.
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.
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:
Select the EntityDataSource control named EntDataSrc_OrderDetails on the design surface of the Default.aspx page.
Click the > symbol on the EntityDataSource control.
Click Configure Data Source.
When the Configure Object Context dialog box appears, select Named Connection AdventureWorksEntities from the drop-down list.
From the DefaultContainerName drop-down list, select AdventureWorksEntities.
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.
From the Select Statement check boxes, check SalesOrderDetailID, OrderQty, ProductID, UnitPrice, and ModifiedDate.
Right-click the EntityDataSource control and select Properties.
From the Properties list select Where, and click the ellipses at the right side of the text box.
When the Expression Editor dialog box appears, type the following text into the Where Expression text area: it.[SalesOrderID] = @parSalesOrderID.
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.
From the ControlID drop-down list, select labelHeaderIDFromList.
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:
Select ListBox1 in the Web page design surface.
Right-click ListBox1, and select Properties.
In the Properties list, click the events option.
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.
Add the following code to the event handler:
labelHeaderIDFromList.Text = ListBox1.SelectedValue; GridView1.DataSource = EntDataSrc_OrderDetails; GridView1.DataBind();
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.
SP1 feature change.