Introduction
The ASP.NET application that we will create we be a 3 tier application, consisting of a data access layer (DAL), a business logic layer (BLL) and a presentation layer (PL).
We will be using Visual Studio Orcas Beta 1 for this tutorial.
Defining our database schema
Before we jump into coding some C# or ASP.NET we need to setup our database, more importantly we first need a database! If you haven’t already go and grab yourself a copy of SQL Express 2005. I will be using SQL Server Management Studio 2005 to create and define my database; however, you can do all of this using Visual Studio 2005 if you want.
Creating a database
Let’s go ahead and create a database for this tutorial. I’m going to create a database called TodoList.
To create a database right click the Databases folder in the Object Explorer window and click New Database (Figure 4-1).
Figure 4-1: Creating a database

When the New Database window is visible enter TodoList (or any other name you want to use) as the Database name (Figure 4-2).
Figure 4-2: Specifying the database name
Adding the Tasks table
With our database created we will now add a Tasks table. In SQL Server Management Studio right click the Tables folder and select New Table (Figure 4-3).
Figure 4-3: Creating the Tasks table
In the table designer replicate the settings from Figure 4-4.
Figure 4-4: Defining the columns
Note: In Figure 4-4 TaskID is a primary key and identity.
When you save the table, save it as Tasks (Figure 4-5).
Figure 4-5: Saving the table
Creating the stored procedures
We need to define three stored procedures, one for adding a new task, one for updating the state of a task, and finally one for retrieving all tasks of a specific state (either active, or closed).
Creating a stored procedure is easy, simply right click on the Stored Procedures folder and click New Stored Procedure (Figure 4-6).
Figure 4-6: Creating a stored procedure
Below are the three code listings for the stored procedures we will use.
Figure 4-7: AddTask
Figure 4-8: GetTasksByState
Figure 4-9: UpdateTaskState
Setting up our data access layer
With our database ready let’s go ahead and create our DAL. We will use the LINQ to SQL file designer in Visual Studio Orcas to do this.
Note: You will first need to add a connection to the TodoList database in the Server Explorer window.
Right click on your website project and add a new LINQ to SQL file called TodoList (Figure 4-10).
Figure 4-10: Adding a LINQ to SQL file to our project

With the file created drag and drop the Tasks table and the three stored procedures we created on to the design canvas of the designer (Figure 4-11).
Figure 4-11: Adding our table and sprocs to our DAL

That’s it! Our DAL is all ready to use.
Adding a business logic layer
In this tutorial there is not much point to adding a BLL, but we will add one nonetheless.
This layer in a real life application this layer would enforce any business rules (parameter checking etc...). Also in a real life application you would want to separate your DAL and BLL into their own components.
Right click the App_Code folder and add a new class called TasksBll.cs, then copy and paste the class definition given in Figure 4-12 into that file.
Figure 4-12: TasksBll.cs
Each method of the TasksBll.cs (Figure 4-12) performs the appropriate operation by accessing methods defined in our DAL.
Enter ASP.NET...
As previously mentioned we will use ASP.NET to define the UI.
The UI will consist of 3 controls:
- DropDownList
- GridView
- FormView
Before we begin add a new AJAX Web Form to your project, the name you give it is irrelevant. Add all the following code snippets inside the form tag of the page.
We have two states
Either a task is active or complete. We will use a drop down list control to store these values (Figure 4-12).
Figure 4-13: DropDownList containing the tasks states
Adding an ObjectDataSource
Before we add any more controls to our web page we need to hook our presentation layer up with our BLL. To do this we will add an ObjectDataSource and wire it up to the appropriate methods defined in our BLL.
Figure 4-14: Adding an ObjectDataSource
Using a GridView to view the tasks
We will now go ahead and add a GridView to our page (Figure 4-15).
Note: I have customized the Item and Edit templates for the checkbox control.
Figure 4-15: Adding a GridView to our page to display the tasks
Using the FormView to add more tasks
To add a new task we will use a FormView (Figure 4-16).
Note: I have not included any data input checking to keep the example as simple as possible.
Figure 4-16: Adding a FormView to allow the user to add new tasks
Adding AJAX functionality to the GridView and the FormView controls
Just to smooth things up I’ve added a few UpdatePanel’s to the page, each wrapping the GridView and FormView respectively.
Figure 4-17: The updated GridView
Figure 4-18: The update FormView
And on that note we are finished! You can download the full source code at the end of this tutorial.
The experience
From personal experience coding the data access layer can be a very boring process – in this tutorial we have seen how effective LINQ to SQL can be when creating our DAL.
Visual Studio Orcas has introduced a few really cool tools to further assist with the creation of the DAL – we no longer need to spend hours on OR mapping. The great thing about LINQ is the ability to further query your DAL, and create some anonymous type.
There is no doubt in my mind that LINQ is set to revolutionize the way programmers interact with data, more so is the fact that we always use a familiar set of standard query operators!
If you download and run the source you will see the site shown in Figure 4-19.
Figure 4-19: Our to-do list web site

Summary
This tutorial showed you how to update the to-do list application Scott Guthrie did some time ago to use LINQ.
I really hope that this tutorial has shown you how simply you can build an application using LINQ, and I really do encourage you to play around with the sample code and further extend the application.
References
Introducing LINQ – Part 1
Introducing LINQ – Part 2
Introducing LINQ – Part 3
Introducing LINQ – Part 4
Introducing LINQ – Part 5
Downloads
TaskList
Top Articles in this category
Introducing LINQ – Part 1
Introducing LINQ is the first part of a series of articles on Language Integrated Query (LINQ). This series will cover the core essentials of LINQ and its use with other technologies like ASP.NET, Win Forms and WPF.
Polymorphism and Encapsulation
Polymorphism and encapsulation are two big words in OO development, and are also a fundamental concept of software development. This article will demystify these concepts by showing you some real world examples.
Writing an ActiveX control in C#
An ActiveX control is an object that supports a customizable programmatic interface. Using the methods, events and properties exposed by the control, web developers can automate their web pages to give the functionality which is equivalent to that of a desktop application.
Introducing LINQ – Part 2
In the first part of this series I introduced you to the new language enhancements in C# 3.0, in this part we will look at querying relational data.
C# 3.0: An Introduction
In this article we will look at some of the language enhancements in C# 3.0. Throughout the article we will use code examples to demonstrate these new enhancements and how to apply them to real world problems. This article will give you the core understanding you will require to further explore the C# 3.0 language using many of its new features.
|
|
Please login to rate or to leave a comment.