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:

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:

user interface layer

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:

Examples

Advantages of 3-Tier Architecture

Disadvantages of 3-Tier Architecture

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:

datainfo

In the following, I rovided code for all the classes and web forms.

Step 1

  1. create database skyworld
  2. use skyworld
  3. 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))
  4. select * from myregisterdata
  5. create procedure userdata
  6. (@spfirstname varchar(30),@splastname varchar(30),@spusername varchar(30),@sppassword varchar(30),@sphouse varchar(30),@spphone varchar(30))
  7. as
  8. begin
  9. insert into myregisterdata values (@spfirstname,@splastname,@spusername,@sppassword,@sphouse,@spphone)
  10. end
  11. go

Step 2

registration

RegisterData.aspx Code

  1. <table style="height: 304px; width: 393px">
  2. <tr>
  3. <td>First Name</td>
  4. <td>:</td>
  5. <td><asp:TextBox ID="txtfirst" runat="server" ></asp:TextBox></td>
  6. </tr>
  7. <tr>
  8. <td>Last Name</td>
  9. <td>:</td>
  10. <td><asp:TextBox ID="txtlast" runat="server" ></asp:TextBox></td>
  11. </tr>
  12. <tr>
  13. <td>User Name</td>
  14. <td>:</td>
  15. <td><asp:TextBox ID="txtuser" runat="server" ></asp:TextBox></td>
  16. </tr>
  17. <tr>
  18. <td>Password</td>
  19. <td>:</td>
  20. <td><asp:TextBox ID="txtpassword" runat="server" ></asp:TextBox></td>
  21. </tr>
  22. <tr>
  23. <td>Address</td>
  24. <td>:</td>
  25. <td><asp:TextBox ID="txtaddress" runat="server" ></asp:TextBox></td>
  26. </tr>
  27. <tr>
  28. <td>Phone No</td>
  29. <td>:</td>
  30. <td><asp:TextBox ID="txtphoneno" runat="server" ></asp:TextBox></td>
  31. </tr>
  32. <tr>
  33. <td></td><td></td>
  34. <td><asp:Button ID="btnsubmit" runat="server" Text="Submit"
  35. onclick="btnsubmit_Click" /> <asp:Button ID="btnreset"
  36. runat="server" Text="Reset" onclick="btnreset_Click" /></td>
  37. </tr>
  38. </table>

RegisterData.aspx.cs Code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using BuninessLayer;
  8. using dataLayer;
  9. namespace Registration3Tier
  10. {
  11. public partial class RegisterData : System.Web.UI.Page
  12. {
  13. objectdataBus dataregister = new objectdataBus();
  14. studentlist studentdata = new studentlist();
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. }
  18. protected void btnreset_Click(object sender, EventArgs e)
  19. {
  20. txtfirst.Text = "";
  21. txtlast.Text = "";
  22. txtuser.Text = "";
  23. txtpassword.Text = "";
  24. txtaddress.Text = "";
  25. txtphoneno.Text = "";
  26. }
  27. protected void btnsubmit_Click(object sender, EventArgs e)
  28. {
  29. studentdata.FirstName = txtfirst.Text;
  30. studentdata.LastName = txtlast.Text;
  31. studentdata.UserName = txtuser.Text;
  32. studentdata.password = txtpassword.Text;
  33. studentdata.Address = txtaddress.Text;
  34. studentdata.PhoneNumber = txtphoneno.Text;
  35. try
  36. {
  37. string result = dataregister.record_insert(studentdata);
  38. if (result != null)
  39. {
  40. Label1.Text = "inserted Successfully";
  41. }
  42. else
  43. {
  44. Label1.Text = "Not registerd Successfully";
  45. }
  46. }
  47. //catch (Exception info)
  48. //{
  49. // throw info;
  50. //}
  51. finally
  52. {
  53. studentdata = null;
  54. }
  55. }
  56. }
  57. }

Step 3

Modify the BL layer BuninessLayer.cs as below:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using dataLayer;
  6. namespace BuninessLayer
  7. {
  8. public class objectdataBus
  9. {
  10. dataLayer.datainfo datanew = new dataLayer.datainfo();
  11. public string record_insert(studentlist abc)
  12. {
  13. try
  14. {
  15. return datanew.registration_details(abc);
  16. }
  17. catch (Exception e)
  18. {
  19. throw e;
  20. }
  21. finally
  22. {
  23. datanew = null;
  24. }
  25. }
  26. }
  27. }

Step 4

Modify the DL Layer DataLayer.cs code as below:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Configuration;
  8. namespace dataLayer
  9. {
  10. public class datainfo
  11. {
  12. string dataconfig = ConfigurationManager.ConnectionStrings["configdata"].ToString();
  13. SqlConnection sqlcon;
  14. SqlCommand sqlcmd;
  15. SqlDataAdapter sqldap;
  16. DataSet ds;
  17. public string registration_details(studentlist user_details)
  18. {
  19. sqlcon = new SqlConnection(dataconfig);
  20. sqlcmd = new SqlCommand();
  21. sqlcmd.Connection=sqlcon;
  22. sqlcmd.CommandType=CommandType.StoredProcedure;
  23. sqlcmd.CommandText="userdata";
  24. sqlcon.Open();
  25. try
  26. {
  27. sqlcmd.Parameters.AddWithValue("@spfirstname",user_details.FirstName);
  28. sqlcmd.Parameters.AddWithValue("@splastname",user_details.LastName);
  29. sqlcmd.Parameters.AddWithValue("@spusername",user_details.UserName);
  30. sqlcmd.Parameters.AddWithValue("@sppassword",user_details.password);
  31. sqlcmd.Parameters.AddWithValue("@sphouse",user_details.Address);
  32. sqlcmd.Parameters.AddWithValue("@spphone",user_details.PhoneNumber);
  33. return sqlcmd.ExecuteNonQuery().ToString();
  34. }
  35. catch (Exception showerror)
  36. {
  37. throw showerror;
  38. }
  39. finally
  40. {
  41. sqlcmd.Dispose();
  42. sqlcon.Dispose();
  43. }
  44. }
  45. }
  46. }

Write the C# property codes to get and set the data as given below:

  1. public class studentlist
  2. {
  3. public string FirstName { set; get; }
  4. public string LastName { set; get; }
  5. public string UserName { set; get; }
  6. public string password { set; get; }
  7. public string Address { get; set; }
  8. public string PhoneNumber { set; get; }
  9. }

Output

table result


Or:

  1. using System;
  2. using System.Linq;
  3. using System.Web;
  4. /// <summary>
  5. /// Summary description for Business_Object
  6. /// </summary>
  7. public class Business_Object
  8. {
  9. //Declared Registration Variables
  10. private string user_name;
  11. private string password;
  12. private string qualification;
  13. private string age;
  14. private string mobile;
  15. //use propertis concepts in C#,to get and set the value in variables
  16. public string Username_value
  17. {
  18. get
  19. {
  20. return user_name;
  21. }
  22. set
  23. {
  24. user_name = value;
  25. }
  26. }
  27. public string pass_value
  28. {
  29. get
  30. {
  31. return password;
  32. }
  33. set
  34. {
  35. password = value;
  36. }
  37. }
  38. public string Qualification_value
  39. {
  40. get
  41. {
  42. return qualification;
  43. }
  44. set
  45. {
  46. qualification = value;
  47. }
  48. }
  49. public string Age_value
  50. {
  51. get
  52. {
  53. return age;
  54. }
  55. set
  56. {
  57. age= value;
  58. }
  59. }
  60. public string mobile_value
  61. {
  62. get
  63. {
  64. return mobile;
  65. }
  66. set
  67. {
  68. mobile = value;
  69. }
  70. }
  71. }