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
Now open visual studio 2008 and create a new website by:
After that create the connection with the database by adding the connection string in the web.config file as follows:
Now create an aspx page through which we will be adding new courses into the table. The markup will look like this:
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
In the App_Code, right click on DAL and select Add New Item and Select Class. Name it
This Class will look like this:
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
This class will be like this:
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:
Then write a method to pass the course name through the object to BAL like this:
Call this method on the click event of the button click like this :
Here I have called the JavaScript function after insert just to alert the user that the course has been added successfully.
Chillas
Regards:
Mudassir
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)
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:
Code:
<appSettings>
<add key="ConnString" value="server=MUDASSIR;database=dbCourse;Integrated Security=true;"></add>
</appSettings>
<connectionStrings>
<remove name="LocalSqlServer" />
<remove name="LocalMySqlServer" />
</connectionStrings>
Code:
<html>
<body>
<table>
<tr>
<td>
<asp:Label ID="lblName" Text="Course Name" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server" BorderStyle="Solid" BorderWidth="1px" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtName" ErrorMessage="*" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" height="50px"></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnAdd" Text="Add Course" runat="server" CausesValidation="true" OnClick="btnAdd_Click" />
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="false" OnClick="btnCancel_Click" />
</td>
</tr>
</table>
</body>
</html>
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
CoursesTransact ion.cs:This Class will look like this:
Code:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
/// Summary description for CoursesTransaction
/// </summary>
public class CoursesTransaction
{
#region variables
public Int64 CourseID { get; set; }
public string CourseDesc { get; set; }
#endregion
public CoursesTransaction()
{
//
// TODO: Add constructor logic here
//
}
public void InsertCourse()
{
SqlConnection Conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
SqlCommand cmd = new SqlCommand("[dbo].[SP_CoursesInsertUpdate]", Conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CourseDesc", CourseDesc);
try
{
Conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally {
Conn.Close();
}
}
}
Right click on the BAL folder and select Add New Item and Add Class. Name it
CoursesTransact ionBAL.cs.This class will be like this:
Code:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for CoursesTransactionBAL
/// </summary>
public class CoursesTransactionBAL
{
#region variables
public Int64 CourseID { get; set; }
public string CourseDesc { get; set; }
#endregion
CoursesTransaction obj = new CoursesTransaction();
DataTable dt = new DataTable();
public CoursesTransactionBAL()
{
//
// TODO: Add constructor logic here
//
}
public void Insert()
{
obj.CourseDesc = CourseDesc;
obj.InsertCourse();
}
}
Switch to code behind file of the AddCourse.aspx
and create an object of the Class like this:
Code:
CoursesTransactionBAL obj = new CoursesTransactionBAL();
Code:
private void Insert()
{
obj.CourseDesc = txtName.Text;
obj.Insert();
}
Code:
protected void btnAdd_Click(object sender, EventArgs e)
{
Insert();
ClientScript.RegisterStartupScript(Page.GetType(), "OnLoad", "Alert();", true);
}
Chillas
Regards:
Mudassir