Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Performing CRUD and Grouping Operations with the LinqDataSource Control

Build fully-editable Create, Read, Update, and Delete (CRUD) database front-ends easily and discover how to group and aggregate data using the new LinqDataSource control in ASP.NET.


advertisement
n a previous DevX article, you saw the basic process for using the LinqDataSource control to retrieve and display data. That article discussed functionality such as passing parameters to a LINQ query and executing a stored procedure. This article builds on that foundation by discussing more sophisticated features for creating fully-editable pages, such as adding Create, Read, Update, and Delete (CRUD) capabilities, and handling CRUD events generated by the LinqDataSource control. In addition, this installment also demonstrates how to aggregate and group data using the LinqDataSource control.

CRUD with LinqDataSource
To begin, create a new Visual C# web site named LinqDataSourceExample. After creating the web site, select Website → Add New Item from the menu. In the Add New Item dialog box, select the "LINQ to SQL Classes" template, and name it AdventureWorksDatabase.dbml. You'll see the Object Relational Designer view of the newly created class. From that view, select View → Server Explorer from the menu, and add a new data connection to the AdventureWorks database. Finally, to create the corresponding entity classes, drag and drop the Production.ProductCategory, Production.ProductSubcategory, and Production.Product tables onto the Object Relational Designer from the Tables node in Server Explorer.

The next step is to create an ASP.NET page that uses the LinqDataSource control to create CRUD capabilities on top of the Product table in the AdventureWorks database. Create a new ASP.NET page named CRUD_Example.aspx and modify its code as follows:

<%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Using LinqDataSource Control for Create, Read, Update and Delete Operations</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView DataSourceID="productSource" DataKeyNames="ProductID" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" AllowPaging="true" AutoGenerateInsertButton="true" ID="productsView" runat="server"> </asp:DetailsView> <asp:LinqDataSource ID="productSource" runat="server" ContextTypeName="AdventureWorksDatabaseDataContext" TableName="Products" EnableUpdate="true" EnableInsert="true" EnableDelete="true"> </asp:LinqDataSource> </div> </form> </body> </html>

The LinqDataSource control in the preceding code has three properties: EnableInsert, EnableUpdate, and EnableDelete. All three are set to true to enable automatic insert, update, and delete support. You need to enable the same functionality at the data-bound control, DetailsView in this case. To accomplish this, you set the AutoGenerateInsertButton, AutoGenerateEditButton, and AutoGenerateDeleteButton of the DetailsView control to true. That's all you need to do to enable CRUD operations with the LinqDataSource control.

Figure 1 shows the output produced by navigating to the page in the browser.

 
Figure 1. CRUD-Enabled LinqDataSource Control: After enabling CRUD operations for the LinqDataSource Control data bound to the DetailsView control, the Edit, Delete, and New buttons appear at the bottom of the control.
 
Figure 2. Editing Data: To enable editing, the control replaces the read-only fields with text boxes where you can enter the product details and adds an Update button that saves changes back to the database.

If you click the Edit button in Figure 1, you'll see the output shown in Figure 2.

When you click the Update button, the LinqDataSource control automatically retrieves the values from the text boxes and invokes the appropriate methods of the Entity class to update the data in the Product table.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap