By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,913 Members | 1,356 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

3-Tier Architecture Explained

P: 85
In this article I am going to explain 3 tier architecture in detail. As it might help out someone who is new to ASP.net.

Consider a simple scenario where we are going to add just course names into the database using three layered architecture.

First of all create a table in the database with columns
CourseID bigint, (Primary key, auto increment)
CourseDesc Varchar(100)
Now open visual studio 2008 and create a new website by:
File-->New-->Web Site
After that create the connection with the database by adding the connection string in the web.config file as follows:
Expand|Select|Wrap|Line Numbers
  1.   <appSettings>
  2.     <add key="ConnString" value="server=MUDASSIR;database=dbCourse;Integrated Security=true;"></add>
  3.  
  4.   </appSettings>
  5.   <connectionStrings>
  6.     <remove name="LocalSqlServer" />
  7.     <remove name="LocalMySqlServer" />
  8.   </connectionStrings>
  9.  
Now create an aspx page through which we will be adding new courses into the table. The markup will look like this:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.   <table>
  4.     <tr>
  5.       <td>
  6.         <asp:Label ID="lblName" Text="Course Name" runat="server"></asp:Label>
  7.       </td>
  8.       <td>
  9.         <asp:TextBox ID="txtName" runat="server" BorderStyle="Solid" BorderWidth="1px" ></asp:TextBox>
  10.         <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtName" ErrorMessage="*" SetFocusOnError="true"></asp:RequiredFieldValidator>
  11.       </td>
  12.     </tr>
  13.     <tr>
  14.       <td colspan="2" height="50px"></td>
  15.     </tr>
  16.     <tr>
  17.       <td colspan="2">
  18.         <asp:Button ID="btnAdd" Text="Add Course" runat="server" CausesValidation="true" OnClick="btnAdd_Click" />
  19.         <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="false" OnClick="btnCancel_Click" />
  20.       </td>
  21.     </tr>
  22.   </table>
  23. </body>
  24. </html>
  25.  
Here we add a text box and two buttons.

Right click on the App_Code in the Solution explorer and add 2 new folders named DAL and BAL. The DAL folder will contain the classes that will be interacting with the database directly and the BAL folder will contain the classes that will work as bridge between the DAL and the Presentation layer(front end).

In the App_Code, right click on DAL and select Add New Item and Select Class. Name it CoursesTransaction.cs:

This Class will look like this:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Data.SqlClient;
  13.  
  14. /// <summary>
  15. /// Summary description for CoursesTransaction
  16. /// </summary>
  17. public class CoursesTransaction
  18. {
  19.     #region variables
  20.     public Int64 CourseID { get; set; }
  21.     public string CourseDesc { get; set; }
  22.     #endregion
  23.     public CoursesTransaction()
  24.     {
  25.         //
  26.         // TODO: Add constructor logic here
  27.         //
  28.     }
  29.  
  30.     public void InsertCourse()
  31.     {
  32.         SqlConnection Conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
  33.         SqlCommand cmd = new SqlCommand("[dbo].[SP_CoursesInsertUpdate]", Conn);
  34.         cmd.CommandType = CommandType.StoredProcedure;
  35.         cmd.Parameters.AddWithValue("@CourseDesc", CourseDesc);
  36.         try
  37.         {
  38.             Conn.Open();
  39.             cmd.ExecuteNonQuery();
  40.         }
  41.         catch (Exception ex)
  42.         {
  43.             throw new Exception(ex.Message);
  44.         }
  45.         finally {
  46.             Conn.Close();
  47.         }
  48.     }
  49.  
  50. }
  51.  
  52.  
This class just contains a function that inserts the new course in the database.

Right click on the BAL folder and select Add New Item and Add Class. Name it CoursesTransactionBAL.cs.

This class will be like this:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12.  
  13. /// <summary>
  14. /// Summary description for CoursesTransactionBAL
  15. /// </summary>
  16. public class CoursesTransactionBAL
  17. {
  18.     #region variables
  19.     public Int64 CourseID { get; set; }
  20.     public string CourseDesc { get; set; }
  21.     #endregion
  22.     CoursesTransaction obj = new CoursesTransaction();
  23.     DataTable dt = new DataTable();
  24.     public CoursesTransactionBAL()
  25.     {
  26.         //
  27.         // TODO: Add constructor logic here
  28.         //
  29.     }
  30.  
  31.     public void Insert()
  32.     {
  33.         obj.CourseDesc = CourseDesc;
  34.         obj.InsertCourse();
  35.     }
  36.  
  37. }
  38.  
  39.  
This class calls the method of the DAL's Class and it takes the Course Desc from the presentation layer which is shown in the next step.

Switch to code behind file of the AddCourse.aspx
and create an object of the Class like this:
Expand|Select|Wrap|Line Numbers
  1.  CoursesTransactionBAL obj = new CoursesTransactionBAL();
  2.  
Then write a method to pass the course name through the object to BAL like this:
Expand|Select|Wrap|Line Numbers
  1. private void Insert()
  2.     {
  3.         obj.CourseDesc = txtName.Text;
  4.         obj.Insert();
  5.     }
  6.  
Call this method on the click event of the button click like this :
Expand|Select|Wrap|Line Numbers
  1.  protected void btnAdd_Click(object sender, EventArgs e)
  2.     {
  3.         Insert();
  4.         ClientScript.RegisterStartupScript(Page.GetType(), "OnLoad", "Alert();", true);
  5.  
  6.     }
  7.  
Here I have called the JavaScript function after insert just to alert the user that the course has been added successfully.


Chillas
Regards:
Mudassir
Aug 2 '12 #1
Share this Article
Share on Google+