Revamping Web Applications: Adding DAL (Data Access Layer)

Read – Revamping Web Applications part 5: Proposed Enhancements

As a part of following best practice we need to separate the data access from our existing project. Here, we talk about how we create an ORM (Object-Relational Mapping) and create a separate DAL.


  1. Remove any Projects which might be unavailable.
  2. We must reconfigure our Connection String to the localhost within Web.Config file.
  3. Any connections to the TFS server shall be currently closed, because our development will be focused initially on local server.
  4. Build the Solution and execute it to check for any errors exist prior to the changes.

To begin with the process we add a class library to the existing EventNetworking (EN) web application.

  1. In the Solution Explorer tab, right click on ‘Solution’ and Add -> New Project
  2. Create a new ‘Class Library’ and name it “EventNetworking.Data”imageMake sure to change the target framework similar to the same version as the existing project. This is a very common mistake that a developer encounters.
  3. Right click on EventNetworking.Data->Properties. In our case we need to change the Target framework to 4.5Next we need to add an App.Config file and set the “ConnectionStrings”.
    We might get the following Output when adding the App.Config
    “Could not parse the configuration file. The error message is: ‘Could not find file ‘D:\Telerik\ZipFiles\EventNetworkCS\EventAdmin\Web.config’.’. Try editing the file manually and then saving it.”
    We should copy the connection string from Web.Config file of the existing project.
    Once we have done that, we again need to go to the Properties of new project and check the “Settings” tab to see if the connection string has been validated.
  4. Now we will transfer the existing data layer file to our new project. Transfer the “Networking.dbml” from “EventNetworking” project to EventNetworking.Data. DBML stands for DataBase Markup Language. The DBML file represents the data model for a LINQ to SQL project – it’s used to generate the C# code for the various entities. And build the “EventNetworking” project to get a list of errors. These errors have occurred because we have moved the DBML file to another project.
  5. To resolve the above errors, first we need to add a reference to the EN project.
    Right click the “EventNetworking” and Add Reference -> Projects -> Select “EventNetworking.Data”. image
    Next, we will have to refactor the code as all the data links have been shifted from its current location to a new project. clip_image001
    Select “using EventNetworking.Data;” for all such errors.
    Rebuild the project to remove errors depending on the above dependency.
  6. We create a new class within “EventNetworking.Data” named “ENData.cs” and copied all the dependent files from “Utilities.cs” within “EventNetworking”.
    We also have to add references in the project for the following assemblies:

    using System.Data.Services.Client;
    using System.Data.Entity;


  7. Remove Utilities from the EN project. Don’t forget to build the project and check for silly errors. Shortcut to build the “current project” is Ctrl+Shift+B. We would still see some error. These errors are produced because there is a discrepancy between the TaskList as a class and a file. So wherever we get an error saying such an element does not exist. Append the using statements with:using EventNetworking.Data;
    using DataEntities = EventNetworking.Data;What these two lines would do is help differentiate between the TypeCollision caused by TaskList as file from the current project and TaskList as class from EventNetworking.Data. How to do that:
    Instead of

    TaskList[] tl = new TaskList[2];

    we alias

    DataEntities.TaskList[] tl = new DataEntities.TaskList[2];

    and make the above changes at all places where TaskList is being referred as class. Now we are left with around 20 errors and all of them having the same construct.
    We will have to make these changes in more than one aspx file.

  8. Finally, we have resolved all the build time errors. The build is a success but there is a runtime error which still lingers.image

This occurs because, we have moved the data access layer to a new project and modified the direct references. But we are still left with the most important reference which is mapping Linq to SQL.
To do so, change the property “ContextTypeName” from the “asp:LinqDataSource” control which is defined as:




Make changes to all the LinqDataSource control in all the aspx files. Build the project and we should be left with neither compile time nor runtime errors.


We have managed to create a separate DAL (Data Access Layer) and reproduce it into a new project named “EventNetworking.Data”. This blog post explains the various errors we faced while we moved the data layer from one project to another and how to resolve these issues.

Read-Revamping Web Applications Part-7 : Adding DAL (Data Access Layer) Part 2


3 thoughts on “Revamping Web Applications: Adding DAL (Data Access Layer)

  1. Pingback: Revamping Web Applications: Adding DAL (Data Access Layer) Part 2 | Telerik Helper

  2. Pingback: Dew Drop – May 30, 2013 (#1,557) | Alvin Ashcraft's Morning Dew

  3. Pingback: Revamping Web Applications: Proposed Enhancements | Telerik Helper

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.