How to Work With Layers in ASP.Net Project

This article explains how to make and work with layers in ASP.NET projects. New developers are often confused about which projects are built first in applications and how to relate them. I will explain the project build order and dependencies. For better understanding you can download this project. I have made a simple project that inserts some records into a database using the following four layers:

  1. Shared (Property Layer): Contains properties in combination with class files.
  2. Data Access Layer (DAL): In this layer I wrote the logic for the data insertion into the database.
  3. Business layer (BAL): The business layer creates a relation between the DAL and the Property Layer.
  4. User Interface (Main Project): This is my startup project where all the ASP pages, Js Files, CSS Files, Images, User controls and so on are.

Note : The connection string is stored in the Web.config file.

See the following image for reference.

asp.net layers

First, run the following query in your database to create the table:

  1. CREATE TABLE [dbo].[TBL_StudentInfo](  
  2.       [Counter] [int] IDENTITY(1,1) NOT NULL,  
  3.       [StudentName] [varchar](200) NULL,  
  4.       [RollNo] [varchar](5) NULL,  
  5.       [Section] [varchar](5) NULL,  
  6.       [SchoolName] [varchar](200) NULL,  
  7.       [ClassTeacher] [varchar](200) NULL  
Use the following procedure to create a sample Web application in Layers:

  • User Interface: "Microsoft Visual Studio" -> "New" -> "Project" -> "Web" -> "ASP.NET Web Application" then give the project the name "UserInterface".
  • Shared: Right-click on the solution then select "userinterface" -> "New project" -> "Visual C#" -> "Class library" then give the project the name "Shared".
  • DataAccessLayer: Create the same as Shared.
  • BusinessLayer: Create the same as Shared.

Now build every project sequencially as mentioned in the Project build order in the image above. Then you need to add DLLs depending on project dependencies; it is also mentioned in that image.

Now your project; the layers are showing like:

layers in asp.net
Now see the following code starting with Shared.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Shared  
  6. {  
  7.     public class TestProperty  
  8.     {  
  9.         public string sStudentName { getset; }  
  10.         public string iRollNumber { getset; }  
  11.         public string sSection { getset; }  
  12.         public string sSchoolName { getset; }  
  13.         public string sClassTeacher { getset; }   
  14.     }  
  15. }
DATA ACCESS LAYER
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data.SqlClient;  
  6. using System.Configuration;  
  7. using Shared;   
  8. namespace DataAccessLayer  
  9. {  
  10.     public class TestDataAccessLayer  
  11.     {  
  12.         SqlConnection conn;  
  13.         SqlCommand dCmd;   
  14.         public TestDataAccessLayer()  
  15.         {  
  16.             conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString);  
  17.         }   
  18.         public int SaveRecords_DAL(TestProperty objTestProp)  
  19.         {  
  20.             try  
  21.             {  
  22.                 conn.Open();  
  23.                 string sInsQuery = @"Insert into TBL_StudentInfo( StudentName, RollNo, Section, SchoolName, ClassTeacher)  
  24.                 values('" + objTestProp.sStudentName + "','" + objTestProp.iRollNumber + "','" + objTestProp.sSection + "','" + objTestProp.sSchoolName + "','" + objTestProp.sClassTeacher + "') ";  
  25.                 dCmd = new SqlCommand(sInsQuery, conn);  
  26.                 return dCmd.ExecuteNonQuery();  
  27.                 conn.Close();   
  28.             }   
  29.             catch (Exception ex)  
  30.             {  
  31.                 throw;  
  32.             }  
  33.         }  
  34.     }  
  35. }
BUSINESS LAYER
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using DataAccessLayer;  
  6. using Shared;   
  7. namespace BusinessLayer  
  8. {  
  9.     public class TestBusinessLayer  
  10.     {  
  11.         public int SaveRecords_BAL(TestProperty objProp)  
  12.         {  
  13.             TestDataAccessLayer objTestDataAcessLayer = new TestDataAccessLayer();  
  14.             return objTestDataAcessLayer.SaveRecords_DAL(objProp);  
  15.         }  
  16.     }  
  17. }
USER INTERFACE CODE BEHIND OF TEST FORM
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using BusinessLayer;  
  14. using Shared;   
  15. namespace UserInterface  
  16. {  
  17.     public partial class _Default : System.Web.UI.Page  
  18.     {  
  19.         TestBusinessLayer objTestBusinessLayer;  
  20.         TestProperty objTestProp;  
  21.         protected void Page_Load(object sender, EventArgs e)  
  22.         {   
  23.         }   
  24.         protected void btnLogin_Click(object sender, EventArgs e)  
  25.         {  
  26.             try  
  27.             {  
  28.                 objTestBusinessLayer = new TestBusinessLayer();  
  29.                 objTestProp = new TestProperty();  
  30.                 objTestProp.sStudentName = txtStuName.Text.Trim();  
  31.                 objTestProp.iRollNumber = txtRollNumber.Text.Trim();  
  32.                 objTestProp.sClassTeacher = txtClassTeacher.Text.Trim();  
  33.                 objTestProp.sSchoolName = txtSchoolName.Text.Trim();  
  34.                 objTestProp.sSection = txtSection.Text.Trim();   
  35.                 int i = objTestBusinessLayer.SaveRecords_BAL(objTestProp);  
  36.                 lblMsg.Visible = true;  
  37.                 if (i == 1)  
  38.                 {  
  39.                     lblMsg.Text = "Saved Successfully";  
  40.                     txtStuName.Text = "";  
  41.                     txtSection.Text = "";  
  42.                     txtSchoolName.Text = "";  
  43.                     txtRollNumber.Text = "";  
  44.                     txtClassTeacher.Text = "";   
  45.                 }  
  46.                 else  
  47.                 {  
  48.                     lblMsg.Text = "Some error occured";   
  49.                 }    
  50.             }  
  51.             catch (Exception ex)  
  52.             {  
  53.                 throw;  
  54.             }    
  55.         }  
  56.     }  
  57. }
Thanks for reading. If you have found any mistakes in this article concept then please comment. Your comments will be appreciated.


Similar Articles