For more information, visit my site: New AspDotNet Concepts.
Hi friends, I have created a simple registration page using Three-Tier Architecture concepts in ASP.NET. We use Three-Tier Architecture concepts in higher-level companies where there are three separate layers formed. There are three layers in 3-Tier Architecture as given below:
- Presentation Layer (UI Layer)
- Business Access Layer (BAL)
- Data Access Layer (DAL)
Where each layer is uniquely important in the role of the project.
1. Presentation Layer (UI)
The Presentation Layer is nothing but a user interface that every user sees on your computer, mobile and Windows screen. You can say that a design part of any application is known as Presentation Layer. The user can post input and get output on your Presentation Layer only. The ASP.NET .axpx file is known as a the Presentation Layer.
2. Business Access Layer (BAL)
The Business Access Layer acts as a mediator layer between the Presentation layer and the Data Access layer. This layer is used to transfer the data between the Presentation Layer and Data Access Layer. This layer is mainly used for Validations and calculations purposes. Every validation and calculation of data is held on that layer only. I have also implemented Property layer or Entity Layer concepts in Business Access Layer. It is an optional layer if you are working on small project. But if you are working on large projects then you need to include this layer in your 3-Tier Architecture Applications. It is used to enhance the security and prevent to brokering the application.
3. Data Access Layer (DAL)
This layer only communicates with the Business Access Layer. The Data Access Layer contains the methods that helps a Business Access Layer. The Business Layer class's methods call the Data Access Layer Class methods to do some required actions with database such as insertion, deletion, updates and so on. All database related connection codes are written in this layer only such as SQL query, stored procedure and so on.
You can easily understand the exact concepts of 3-Tier Applications as shown below:
What are the working process of 3 Layers
When any user post data from your presentation layer (user Interface layer) --> then this data first goes to the Business Access Layer then the validation and calculation is done on this layer then this data passes data to the Data Access Layer then the Data Access Layer fetches the required data or inserts the data into the database. Then the Data Access Layer passes the required data to the business Access Layer. Then the Business Access Layer sends the required data to the Presentation Layer. Then the Presentation Layer is responsible for displaying the required data to the user's computers or mobiles or Windows.
Why we use 3-Tier Architecture
There are the following reasons to use 3-Tier Architecture in our ASP.NET applications:
- To increase the security in application.
- To easily maintain the application.
- To easily modify or change in application.
- To reduce the server over load.
- To reduce the loading time of the application.
Examples
- All companies working in a software environment always use 3-Tier Architecture concepts in their applications, whether they are working on small projects or large projects. if you are using 3-Tier concepts then it will be more complex but more understandable to the users.
- Suppose you are working on a project in a team of 1000 members. There are many members shifted day by day due to some problems. When any new member is assigned for this project, then he can easily understand the concept of 3-Tier Architecture and be involved in the project. If the companies are working on a 1-Tier Architecture then the shifting of an employee from one project to another project is not possible.
- In 3-Tier, you can easily update any tier of code. But in 1-Tier, it is not possible.
Advantages of 3-Tier Architecture
- Each layer always uses your separate code, so it is easy to modify the code.
- It is helpful to easily maintain and understand the large projects.
- You can easily change your graphical environment.
- It is more secure because users can't access the database directly.
- You can easily change any layer of code without affecting the other two layers.
- It is a more consistent application.
Disadvantages of 3-Tier Architecture
- Its takes more time to build.
- Many people encounter problems because they lack good knowledge of OOP concepts and other C# programming such as class, object, property and so on.
- 3-Tier Architecture is more complex to build.
There are some steps to implement the 3-Tier Architecture concepts in ASP.NET applications. In this application, I will explain how to build a 3-Tier registration and login page in ASP.NET as given below:
- I have created this login page using a 3-Tier architecture.
- Inside the BuninessLayer class library, I have created a class named BuninessLayer.cs.
- Inside the DAL class library I created a class named dataLayer.cs.
- Inside the Registration3Tier application I created the web form RegisterData.aspx.
In the following, I rovided code for all the classes and web forms.
Step 1
- create database skyworld
-
- use skyworld
-
- create table myregisterdata(sno int identity(1,1) primary key,firstname varchar(30),lastname varchar(30),username varchar(30),password varchar(30),houseaddress varchar(30),phoneno varchar(30))
-
- select * from myregisterdata
-
- create procedure userdata
- (@spfirstname varchar(30),@splastname varchar(30),@spusername varchar(30),@sppassword varchar(30),@sphouse varchar(30),@spphone varchar(30))
- as
- begin
- insert into myregisterdata values (@spfirstname,@splastname,@spusername,@sppassword,@sphouse,@spphone)
- end
- go
Step 2
RegisterData.aspx Code
- <table style="height: 304px; width: 393px">
- <tr>
- <td>First Name</td>
- <td>:</td>
- <td><asp:TextBox ID="txtfirst" runat="server" ></asp:TextBox></td>
- </tr>
-
- <tr>
- <td>Last Name</td>
- <td>:</td>
- <td><asp:TextBox ID="txtlast" runat="server" ></asp:TextBox></td>
- </tr>
- <tr>
- <td>User Name</td>
- <td>:</td>
- <td><asp:TextBox ID="txtuser" runat="server" ></asp:TextBox></td>
- </tr>
- <tr>
- <td>Password</td>
- <td>:</td>
- <td><asp:TextBox ID="txtpassword" runat="server" ></asp:TextBox></td>
- </tr>
- <tr>
- <td>Address</td>
- <td>:</td>
- <td><asp:TextBox ID="txtaddress" runat="server" ></asp:TextBox></td>
- </tr>
- <tr>
- <td>Phone No</td>
- <td>:</td>
- <td><asp:TextBox ID="txtphoneno" runat="server" ></asp:TextBox></td>
- </tr>
- <tr>
- <td></td><td></td>
- <td><asp:Button ID="btnsubmit" runat="server" Text="Submit"
- onclick="btnsubmit_Click" /> <asp:Button ID="btnreset"
- runat="server" Text="Reset" onclick="btnreset_Click" /></td>
- </tr>
-
- </table>
RegisterData.aspx.cs Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using BuninessLayer;
- using dataLayer;
-
- namespace Registration3Tier
- {
- public partial class RegisterData : System.Web.UI.Page
- {
- objectdataBus dataregister = new objectdataBus();
- studentlist studentdata = new studentlist();
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void btnreset_Click(object sender, EventArgs e)
- {
- txtfirst.Text = "";
- txtlast.Text = "";
- txtuser.Text = "";
- txtpassword.Text = "";
- txtaddress.Text = "";
- txtphoneno.Text = "";
- }
-
- protected void btnsubmit_Click(object sender, EventArgs e)
- {
- studentdata.FirstName = txtfirst.Text;
- studentdata.LastName = txtlast.Text;
- studentdata.UserName = txtuser.Text;
- studentdata.password = txtpassword.Text;
- studentdata.Address = txtaddress.Text;
- studentdata.PhoneNumber = txtphoneno.Text;
-
- try
- {
- string result = dataregister.record_insert(studentdata);
- if (result != null)
- {
- Label1.Text = "inserted Successfully";
- }
- else
- {
- Label1.Text = "Not registerd Successfully";
- }
-
- }
-
- //catch (Exception info)
- //{
- // throw info;
- //}
- finally
- {
- studentdata = null;
- }
- }
- }
- }
Step 3
Modify the BL layer BuninessLayer.cs as below:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using dataLayer;
-
- namespace BuninessLayer
- {
- public class objectdataBus
- {
-
- dataLayer.datainfo datanew = new dataLayer.datainfo();
-
- public string record_insert(studentlist abc)
- {
- try
- {
- return datanew.registration_details(abc);
- }
- catch (Exception e)
- {
- throw e;
- }
- finally
- {
- datanew = null;
- }
- }
-
- }
- }
Step 4
Modify the DL Layer DataLayer.cs code as below:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
-
- namespace dataLayer
- {
-
- public class datainfo
- {
-
- string dataconfig = ConfigurationManager.ConnectionStrings["configdata"].ToString();
-
- SqlConnection sqlcon;
- SqlCommand sqlcmd;
- SqlDataAdapter sqldap;
- DataSet ds;
-
- public string registration_details(studentlist user_details)
- {
- sqlcon = new SqlConnection(dataconfig);
- sqlcmd = new SqlCommand();
- sqlcmd.Connection=sqlcon;
- sqlcmd.CommandType=CommandType.StoredProcedure;
- sqlcmd.CommandText="userdata";
- sqlcon.Open();
-
- try
- {
- sqlcmd.Parameters.AddWithValue("@spfirstname",user_details.FirstName);
- sqlcmd.Parameters.AddWithValue("@splastname",user_details.LastName);
- sqlcmd.Parameters.AddWithValue("@spusername",user_details.UserName);
- sqlcmd.Parameters.AddWithValue("@sppassword",user_details.password);
- sqlcmd.Parameters.AddWithValue("@sphouse",user_details.Address);
- sqlcmd.Parameters.AddWithValue("@spphone",user_details.PhoneNumber);
- return sqlcmd.ExecuteNonQuery().ToString();
- }
- catch (Exception showerror)
- {
-
- throw showerror;
- }
- finally
- {
- sqlcmd.Dispose();
- sqlcon.Dispose();
- }
-
- }
- }
- }
Write the C# property codes to get and set the data as given below:
- public class studentlist
- {
-
- public string FirstName { set; get; }
- public string LastName { set; get; }
- public string UserName { set; get; }
- public string password { set; get; }
- public string Address { get; set; }
- public string PhoneNumber { set; get; }
- }
Output
Or:
- using System;
- using System.Linq;
- using System.Web;
-
- /// <summary>
- /// Summary description for Business_Object
- /// </summary>
- public class Business_Object
- {
- //Declared Registration Variables
- private string user_name;
- private string password;
- private string qualification;
- private string age;
- private string mobile;
- //use propertis concepts in C#,to get and set the value in variables
-
- public string Username_value
- {
- get
- {
- return user_name;
- }
- set
- {
- user_name = value;
- }
- }
-
- public string pass_value
- {
- get
- {
- return password;
- }
- set
- {
- password = value;
- }
- }
-
- public string Qualification_value
- {
- get
- {
- return qualification;
- }
- set
- {
- qualification = value;
- }
- }
-
- public string Age_value
- {
- get
- {
- return age;
- }
- set
- {
- age= value;
- }
- }
-
- public string mobile_value
- {
- get
- {
- return mobile;
- }
- set
- {
- mobile = value;
- }
- }
- }