Using LINQ in ASP.NET (Part 4)
Introduction
In the Part 1 and
Part 2 of this series we
discussed how to use LINQ to SQL features to query and manipulate data.
Part 3 introduced you
with the inbuilt LINQ to SQL class designer. The LINQ to SQL class designer not
only allows you to design classes visually but also saves reasonable amount of
time otherwise needed to write equivalent code manually. Another such handy
feature available to ASP.NET developers is LINQ Data Source Control. In this
article I will explore this control with examples.
LINQ Data Source Control (LDS)
The data source controls (SQL data source, Object data source and XML data
source) were first introduced in ASP.NET 2.0. Since then they have quickly
became popular amongst developers. With LINQ becoming an integral part of .NET
framework, no wonder that ASP.NET team introduced a LINQ aware data source
control. The LINQ data source control follows the same thought stream as SDS and
ODS but works with LINQ data contexts and entities rather than database or plain
classes. Just like SDS and ODS you can hookup the LINQ data source control to
any data bound controls such as GridView and DetailsView.
Example - Using LDS with DetailsView
In order to see the simplest use of LDS open the same web site that you
developed in Part 3.
This web site already has a custom data context and CEmployees entity class. We
will use them in this and further examples.
Add a new web form to the web site. Drag and drop a LDS on it. From the smart
tag of LDS open the configuration wizard.

The first step allows you to choose a custom data context object. Recollect
that we previously developed a data context with name DataClassesDataContext.

The second step allows you to choose an entity class (i.e. LINQ table) and
its properties to be used.

If you wish to INSERT, UPDATE or DELETE data through the LDS then you should
click on the Advanced button and check the appropriate checkbox.

You can limit the data being fetched using Where Expression dialog opened by
clicking Where button of step 2. In our example, we specified the condition -
Employee <= 20.

Similarly, you can specify the ordering (i.e. sorting) mechanism using
OrderBy Expression dialog. You can open this dialog by clicking on OrderBy
button of step 2. In our exanple we specified that the results are to be ordered
on FirstName column.
The following fragment shows the markup generated for the LDS.
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext"
TableName="CEmployees"
EnableDelete="True"
EnableInsert="True"
EnableUpdate="True"
AutoGenerateOrderByClause="True"
>
<OrderByParameters>
<asp:Parameter Name="P1" DefaultValue="FirstName" />
</OrderByParameters>
</asp:LinqDataSource>
This completes the configuration of the LDS. Now drag and drop a DetailsView
control on the form and set its DataSourceID property to LinqDataSource1. Enable
paging, inserting, updating and deleting for the DetailsView. Run and test the
web form.
Projecting During Selecting Data
In the above example we fetches all the pieces of data i.e. EmployeeID,
FirstName and LastName. If you wish to project the data in some different
fashion you can use Select property. The following example shows how:
Select= new (EmployeeID, FirstName, LastName)
Note, however, that LDS with such projection cannot be used to insert, update
or delete data.
Events of LDS
LDS exposes several events that you can handle to get control of select,
insert, update and delete operations. Here is the list :
- Inserting
- Inserted
- Updating
- Updated
- Deleting
- Deleted
- Selecting
- Selected
As you can see just like SDS and ODS, LDS also follows pre and post event
pattern. The -ing events are raised before the operation takes place and the -ed
events are raised after the operation is over.
Just to get a feeling as to how these events are used see the following code.
protected void LinqDataSource1_Selecting
(object sender, LinqDataSourceSelectEventArgs e)
{
e.OrderByParameters["P1"] = "LastName";
}
protected void LinqDataSource1_Updating
(object sender, LinqDataSourceUpdateEventArgs e)
{
CEmployee oldEmp = (CEmployee)e.OriginalObject;
CEmployee newEmp = (CEmployee)e.NewObject;
if (oldEmp.FirstName == newEmp.FirstName
&& oldEmp.LastName == newEmp.LastName)
{
Label1.Text = "Nothing to change!";
e.Cancel = true;
}
}
We have configured our LDS to sort the records on FirstName property. The
Selecting event handler, however, changes it to LastName using OrderByParameters
collection. Recollect that P1 is the name of the OrderByParameter we added while
configuring the LDS.
The Updating event handler checks if the data has been changed using
OriginalObject and NewObject properties. If not it sets the Cancel property to
true and displays an error message.
That's it for now. Stay tuned!