Search Articles :

HyperLink
AddThis Feed Button
Bookmark and Share



Latest Downloads
Jobs Site Starter Kit for ASP.NET 3.5
Store documents in on-line briefcase
Easy Survey
Share photos with your own Photo Gallery
Web Site File Manager
BinaryIntellect Code Generator (Beta)
Develop your own Blog
E-commerce Starter Kit
Jobs Site Starter Kit
Database Helper for .NET 2.0
 
Latest Blog Posts
Using Extension Methods
Working with Binary Large Objects (BLOBs) Using SQL Server and ADO.NET
भाग 4 : मी नोकरी सोडतो
Using LINQ in ASP.NET (Part 4)
देवाच्या डाव्या हाती (भाग 3)
Using LINQ in ASP.NET (Part 3)
देवाच्या डाव्या हाती (भाग 2)
Using LINQ in ASP.NET (Part 2)
Beginning XML with C# 2008: From Novice to Professional Published!
देवाच्या डाव्या हाती (भाग 1)
 




Hosted By DiscountASP.net
Developer's Guide to ASP.NET 3.5
Master ASP.NET 3.5 development using C# and Visual Studio.NET 2008. Web forms, server controls, data binding, AJAX, ASMX and WCF services and more...
Beat stress - learn Kriya Yoga Meditations
A six week online course in our style of Kriya Yoga is available absolutely FREE. Very few websites teach you the ancient art and science of Kriya Yoga in such a systematic and authentic manner.

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!



Associated Links
Download Source Code

Posted On : 28 Jul 2008
Current Rating :
Rate This Article :

About the Author
Bipin Joshi
Bipin Joshi is the proprietor of BinaryIntellect Consulting where he conducts premier training programs on .NET technologies. He wears many hats including software consultant, mentor, prolific author, webmaster, Microsoft MVP and member of ASPInsiders. Having adopted Yoga way of life Bipin also teaches Kriya Yoga to the interested individuals. His detailed profile can be read at his blog. He can also be reached there.



Copyright (C) BinaryIntellect Consulting. All rights reserved.
Contact Us
Read Terms Of Use