Published: 16 May 2007
By: Granville Barnett

In the previous few parts of this series we have talked a lot about what LINQ can give you, in this part of the series we will use LINQ, ASP.NET and ASP.NET AJAX to replicate the to-do list that Scott Guthrie created a while back to show off the features of ASP.NET AJAX.

Introducing LINQ Series

  • Part 1 In this part you will learn how to the query in-memory collections.
  • Part 2 In this part we will look at querying relational data.
  • Part 3 In this part we will look a little more at what entities are, as well as taking a closer look at the key types we can use and their application.
  • Part 4 In this part of the series we will use LINQ, ASP.NET and ASP.NET AJAX to replicate the to-do list that Scott Guthrie created a while back to show off the features of ASP.NET AJAX.
  • Part 5 In this part of the series I will explain the DataContext class in depth through a series of examples and explanations.
  • 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.

    Downloads

    TaskList

    Introducing LINQ Series

  • Part 1 In this part you will learn how to the query in-memory collections.
  • Part 2 In this part we will look at querying relational data.
  • Part 3 In this part we will look a little more at what entities are, as well as taking a closer look at the key types we can use and their application.
  • Part 4 In this part of the series we will use LINQ, ASP.NET and ASP.NET AJAX to replicate the to-do list that Scott Guthrie created a while back to show off the features of ASP.NET AJAX.
  • Part 5 In this part of the series I will explain the DataContext class in depth through a series of examples and explanations.
  • <<  Previous Article Continue reading and see our next or previous articles Next Article >>

    About Granville Barnett

    Sorry, no bio is available

    This author has published 32 articles on DotNetSlackers. View other articles or the complete profile here.

    Other articles in this category


    C# 4.0 Reflection Programming - Part 2
    As introduced in the first article, the most typically-used tools associated with .NET reflection ar...
    C# 4.0 Reflection Programming - Part 3
    In the previous article, we used the reflection to obtain the information of an assembly, module, ty...
    C# 4.0 Reflection Programming - Part 4
    In this last article of this series, we will learn what to do with reflection. But before making the...
    Understanding and Using Extension Methods
    Extension methods were new to C# 3.0. They allow you to add a method to an existing type without hav...
    Introduction to C# 3.0 features
    C# 3.0 introduced some of very useful features built on top of 2.0. This article explains the usage,...

    You might also be interested in the following related blog posts


    Oredev Wrap-Up read more
    Introducing SharePoint 2010 Training at U2U read more
    The Underground at PDC read more
    10 resources to learn Moq read more
    Building A Product For Real read more
    My History of Visual Studio (Part 6) read more
    BeginDialOut with Office Communicator Clients read more
    DotNetNuke Fusion Results for Q3 read more
    Dynamic in C# 4.0: Introducing the ExpandoObject read more
    GiveCamps Get a new Sponsor read more
    Top
     
     
     

    Discussion


    Subject Author Date
    placeholder Why there is no sorting option for grid view if object datasrouce is using linq in Bll Najam Sikander 6/1/2009 5:00 AM
    Check box instead of DropDown Paxton Krichmar 7/23/2007 9:16 PM
    placeholder RE: Check box instead of DropDown Granville Barnett 9/9/2007 5:48 PM
    Certainly the Best of the Best Examples (and a question) Dick Norman 9/18/2007 6:24 PM
    placeholder Facing same error - Need suggestions Partheepan Mani 4/2/2008 11:02 AM
    Method being used as a type? Doug Leary 4/21/2008 4:15 AM
    placeholder RE: Method being used as a type? Doug Leary 4/21/2008 5:50 AM
    Great article - big question Jason Soares 5/12/2008 8:56 AM
    placeholder Handling errors Tom Wingquist 7/31/2008 9:58 PM
    Amazing series Omar Karim 5/16/2007 8:31 AM
    placeholder Not able to rate Omar Karim 5/16/2007 8:32 AM
    Re: Not able to rate Sonu Kapoor 5/16/2007 8:34 AM

    Please login to rate or to leave a comment.