Browse DevX
Sign up for e-mail newsletters from DevX


Performing CRUD and Grouping Operations with the LinqDataSource Control  : Page 2

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Handling the Updating Event
So, it's easy to set up a LinqDataSource control to update data automatically, but you probably want to pre-process the data before submitting it to the database. You can accomplish this by handling the LinqDataSource control's Updating event. The code example shown below checks the user-entered data to ensure its validity:

<%@ Page Language="C#" %> <script runat="server"> void productSource_Updating(object sender, LinqDataSourceUpdateEventArgs e) { Product oldProduct = (Product)e.OriginalObject; Product newProduct = (Product)e.NewObject; //If the product id has changed, cancel the update event if (oldProduct.ProductID != newProduct.ProductID) { e.Cancel = true; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Using LinqDataSource to handle Update event</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView DataSourceID="productSource" DataKeyNames="ProductID" AutoGenerateEditButton="true" AllowPaging="true" ID="productsView" runat="server"> </asp:DetailsView> <asp:LinqDataSource ID="productSource" runat="server" OnUpdating="productSource_Updating" TableName="Products" ContextTypeName="AdventureWorksDatabaseDataContext" EnableUpdate="true"> </asp:LinqDataSource> </div> </form> </body> </html>

The code sets the LinqDataSource control's OnUpdating attribute to the name of the event handler. This event handler (named productSource_Updating) gets a LinqDataSourceUpdateEventArgs argument that contains various details about the updating event, as shown in Table 1.

Table 1. LinqDataSourceUpdateEventArgs Object: The LinqDataSourceUpdateEventArgs object exposes these properties.
Property Description
Cancel Allows you to cancel the update operation
Exception Allows you to get the generated exception before the update operation
NewObject Allows you to retrieve the data that will be saved in the data source
OriginalObject Allows you to retrieve the data that was originally retrieved from the data source

The preceding code typecasts the values of the OriginalObject and NewObject properties to the Product type. Next, it compares the values of the original and new object ProductID properties; if they differ, it cancels the update operation by setting the Cancel property to true. As the example shows, you have access to both the original and any changed data in this event before committing the changes to the database.

Handling the Deleted Event
In addition to pre-processing events such as Updating, you can also handle post-processing events with the LinqDataSource control. For example, you can display confirmation message to the user after successfully completing a Delete operation. The following page shows an example:

<%@ Page Language="C#" %> <%@ Import Namespace="System.Collections.Generic" %> <script runat="server"> void productCategorySource_Deleted(object sender, LinqDataSourceStatusEventArgs e) { if (e.Exception != null) lblMessage.Text = e.Exception.Message; ProductCategory category = (ProductCategory)e.Result; lblMessage.Text += "The Deleted Product category id is: " + category.ProductCategoryID; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Using LinqDataSource to handle Deleted event</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView DataSourceID="productCategorySource" AutoGenerateRows="false" DataKeyNames="ProductCategoryID" AutoGenerateDeleteButton="true" AllowPaging="true" ID="productCategoriesView" runat="server"> <Fields> <asp:BoundField DataField="ProductCategoryID" ReadOnly="true" HeaderText="Category ID" /> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="ModifiedDate" HeaderText="Last Modified Date" /> </Fields> </asp:DetailsView> <asp:LinqDataSource ID="productCategorySource" runat="server" OnDeleted="productCategorySource_Deleted" TableName="ProductCategories" EnableDelete="true" ContextTypeName="AdventureWorksDatabaseDataContext"> </asp:LinqDataSource> <br /><br /> <asp:Label runat="server" ID="lblMessage" Font-Bold="true" ForeColor="Red" /> </div> </form> </body> </html>

The preceding page code handles the LinqDataSource control's Deleted event. As the name suggests, the Deleted event fires after the delete operation completes, so you could use this event to check whether the deletion was successful and/or perform cleanup activities. Again, the control receives data about the deleted object in a special argument type, the LinqDataSourceStatusEventArgs object. Table 2 summarizes its key properties.

Table 2. LinqDataSourceStatusEventArgs Object: The LinqDataSourceStatusEventArgs object exposes these properties.
Property Description
Exception Allows you to get any exception generated during the operation
ExceptionHandled Lets you indicate whether the exception was handled so that the exception is not thrown back to the client
Result Allows you to get the object that represents the result of the operation

The preceding code first checks to see if any exception occurred during the delete operation, and if so, displays the exception message.

if (e.Exception != null) lblMessage.Text = e.Exception.Message;

Next, it typecasts the e.Result value to a ProductCategory object, and displays its ProductCategoryID value on the label control (see Figure 3).

ProductCategory category = (ProductCategory)e.Result; lblMessage.Text += "The Deleted Product category id is: " + category.ProductCategoryID;

Figure 3. Handling the Deleted Event: By capturing the Deleted event of the LinqDataSource control, you can provide status information to users or display any exception messages generated during the deletion

Thanks for your registration, follow us on our social networks to keep up-to-date