473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

3-Tier Architecture Explained

85 64KB
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
0 23299

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: David Noble | last post by:
We've been developing a web site using 3-tier architecture for 18 months now. There is a common layer that defines the classes - using XML schemas. The data layer acts as a wrapper to 3 databases...
1
by: mike | last post by:
regards: http://www.wretch.cc/album/show.php?i=otp&b=1&f=1111993473&p=2 --------------------------------------------------------------------------- following is my program logic...
4
by: apngss | last post by:
what's the differences between collocated architecture and distributed architecture?? My understanding is that collocated architecture has everything in a single machine? i.e. There is only 1...
6
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that...
4
by: alex | last post by:
I'm setting up a c# client/server application in a 3tier system. Everything from the business objects down through the DAL is stateless. This seems to work great for clustering but the...
5
by: Ludwig Wittgenstein | last post by:
Other than the Design Patterns book, which book(s) is/are the best to learn object-oriented software design/architecture from ?
6
by: carsonbj | last post by:
I have an issue where the below operation works on a little-endian architecture but not on a big-endian architecture. I was under the impression that pointer arithmetic is architecture independant...
13
by: rrs.matrix | last post by:
hi i have to detect the type of CPU. whether it is 32-bit or 64-bit.. how can this be done.. can anyone please help me.. thanks.
0
by: srikar | last post by:
Hi all, I am having a problem, when I am compiling the code in 32 bit option on a 64 bit machine using the macro CPPFLAGS= -m32 I am getting the following warnings from the linker . ...
11
by: Monty | last post by:
Hello, I have a class library of business objects that will accessed both by an ASP.Net front end and a WinForms front-end. A typical business object in class library, let's say "Employee", has...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.