Data binding with Linq to SQL and ASP.NET
Nuestro buen amigo John Olamendy ha escrito un excelente artículo, aquí el detalle:
Data binding with Linq to SQL and ASP.NET
Introduction
Linq to SQL is an ORM technology shipped within Microsoft.NET 3.5 enabling to map the object model of your solution with the relational data schema in the database system. You can also use this technology to query the database and execute transactional operations such as update/insert/delete.
In this article, I will illustrate how to use the new features of ASP.NET 3.5 to support data binding with Linq to SQL, specifically using the <asp:LinqDataSource> control (similar to ObjectDataSource and SQLDataSource) to specify declaratively the data binding from Web controls and the object model in Linq.
Getting started with the solution
In this solution, we’re going to create a Web application for showing a list of products by category. The users will be able to skip the product list using the paging mechanism, and finally the users will be able to edit (in a detail view of the products) and delete products from the list.
This Web application will be implemented using the three-layer approach. The business rules, business process logic and domain model will be in the business logic layer. The Web pages, Web controls and binding mechanism is part of the presentation layer. And finally, the data access logic and the underlying mapping to the application domain model is part of the data access layer. For this application, the ORM technology is Linq to SQL.
Now let’s get started with the solution by opening the Visual Studio.NET 2008 IDE and creating an ASP.NET Web Application for the presentation layer and a Class Library project for the business logic and data access layers. The main data source for this solution is the Production.Product and Production.ProductSubcategory tables in the AdventureWorks database shipped with Microsoft SQL Server 2005/2008.
First step is to define the data model in the Class Library project by adding a “Linq to SQL Classes” artifact (see Figure 1).
Figure 1
Then connect to the back-end database, and define the object model and the underlying mappings to the underlying relational data source (see Figure 2).
Figure 2
Then compile this project and add a reference to it from the Web application and open to the Default.aspx page.
Next step is to add GridView Web control in the Web form and we’re going to use the <asp:linqdatasource> control to bind the object model to the GridView control. Visual Studio.NET has a built-in support for this operation, so we’re going to switch into the design view, select the GridView control, click on the smart tag and then choose the “New data source” option (see Figure 3).
Figure 3
Then choose the Linq option in the “Data Source Configuration Wizard” wizard and click on the OK button (see Figure 4).
Figure 4
In the “Choose a Context Object” page, you can see the available Linq to SQL DataContext classes that the application is defined or is referenced in other assemblies and click on the Next button (see Figure 5).
Figure 5
In the “Configure Data Selection” page, you can select the table, the fields to display and grouping option. You can also define a filter and establish an order of the records. In this case, we’re going to show all the records and fields of the Product table. After this configure, you can click on the Finish button (see Figure 6).
Figure 6
You can see the result in the Figure 7.
Figure 7
In order to define the paging, editing and deleting features on the GridView, you need to click on the smart tag of LinqDataSource object (ldsProducts) and check the “Enable Delete” and “Enable Update” options (see Figure 8).
Figure 8
Then click on the smart tag of the GridView control and check the “Enable Paging”, “Enable Editing” and “Enable Deleting” options (see Figure 9).
Figure 9
See full Article here.
Saludos
Fernando García Loera (Community Program Manager – Latin America Region)