Certified ASP.NET Programmer Learning Resources Configuring the LinqDataSource

Learning Resources
 

Configuring the LinqDataSource


The Configure Data Source - LinqDataSource dialog box helps you configure a LinqDataSource control when you want to use language-integrated query (LINQ) to display data in a data-bound ASP.NET Web server control. The title of the dialog box changes according to the ID of the data source control.

The data must be represented as a class in the App_Code folder of the Web application. You can create a class that represents a database table by adding a LINQ to SQL file to the App_Code folder. You can then drag the table onto the Object Relational Designer. For more information, see Object Relational Designer (O/R Designer).

The Configure Data Source - LinqDataSource dialog box enables you to select which class to use for retrieving data. By default, the Show only DataContext objects check box is selected, which means you can select only from classes that derive from the DataContext class. If there are no classes that derive from DataContext, the Choose your context object list is empty. In that case, you can create a class that derives from DataContext, typically by dragging a database table onto the O/R Designer. Alternatively, you can clear the Show only DataContext objects check box and select a class from the App_Code folder.

To access this dialog box from an ASP.NET Web server control

  • In Visual Studio, open an ASP.NET Web page.
  • Switch to Design view.
  • From the Toolbox, drag a data-bound control onto the page. For example, from the Data group of the Toolbox, drag a GridView control onto the page.
  • Right-click the control and then click Show Smart Tag.
  • The task window is displayed.
  • In the Choose Data Source list, select . The Data Source Configuration Wizard starts.
  • Under Where will the application get data from, click LINQ.
  • In the Specify an ID for the data source box, type a data source ID.
  • Click OK.

A LinqDataSource control is created on the page, and the Configure Data Source - LinqDataSource1 window is displayed.

To access this dialog box directly from a LinqDataSource control

  • In Visual Studio, open or switch to an ASP.NET Web page that contains a LinqDataSource control.
  • Switch to Design view.
  • Right-click the control and then click Show Smart Tag.
  • In the tasks window, click Configure Data Source.
  • The Configure Data Source - LinqDataSource1 window is displayed.

After you configure the LinqDataSource control, you can bind it to a data-bound control by specifying the data source ID in the Choose Data Source list or by setting the DataSourceID property of the Web server control.

Show only DataContext objects
Specifies that only classes that derive from the DataContext class are displayed in the Choose your context object list.

Choose your context object
Specifies the class that represents the data to retrieve. The list displays classes in the App_Code folder. If Show only DataContext objects is selected, only classes that derive from DataContext are displayed. If no classes appear in the list, you must create a class that represents the data. To do this, add a LINQ to SQL file to the App_Code folder, and then drag a database table onto the O/R Designer. LINQ to SQL automatically creates the classes that represent the database and table. The class that represents the database derives from the DataContext class.

Table
Specifies which property or field in the context class represents the data collection. When the context class derives from the DataContext class, only properties that reference an object that implements ITable are displayed in the list. Otherwise, properties that reference an object that implements IEnumerable are displayed in the list.

Group By
Specifies which property to use for grouping the data. To group by more than one value, in the Group By list select (Custom) and then type the expression in the Custom Group box. For more information, see the GroupBy property.

When you select an item in the Group By list, you cannot enable automatic update, insert, or delete operations.

Select
Specifies which values will be returned from the query. The user interface for the Select element depends on whether you have selected options to group data. When the data is not grouped, the Select list includes a * check box for selecting all the properties and a check box for each property in the class that is specified in the Table element. When you select any check box other than the * check box, the Advanced button is disabled. You cannot enable automatic update, insert, or delete operations when a subset of data is selected.

When the Group By element is set, the Select element includes a table for setting the properties and aggregation functions that are returned with the query. The table automatically includes entries for the key and it properties. The key property refers to a dynamically created object that contains the values for the property or properties that are used to group the data. The it object refers to a collection of the individual records within a grouping of data. You can add more properties to the table by selecting a property under Column and then selecting an aggregation function in the Function list. The Alias column enables you to assign a name to the data that is returned from the aggregation function.

 

 For Support