Skip to main content

3-Tier Architecture in ASP.NET with C#

Presentation Layer (UI)
Presentation layer contains pages like .aspx or windows form where data is presented to the user or input is taken from the user.
Business Access Layer (BAL) or Business Logic Layer
BAL contains business logic, validations or calculations related with the data..
Data Access Layer (DAL)
DAL contains methods that helps business layer to connect the data and perform required action, might be returning data or manipulating data (insert, update, delete etc).

Designing 3-Tier Architecture

 

3

Data Access Layer (DAL) Code

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for EmployeeDAL
/// </summary>
public class EmployeeDAL
{
    string connStr = ConfigurationManager.ConnectionStrings["connectionstring"].ToString();
    public EmployeeDAL()
    {
    }
    public int Insert(string E_name, string E_address, int E_age)
    {
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand dCmd = new SqlCommand("insert into EMP values(@name,@add,@age)", conn);
        try
        {
            dCmd.Parameters.AddWithValue("@name", E_name);
            dCmd.Parameters.AddWithValue("@add", E_address);
            dCmd.Parameters.AddWithValue("@age", E_age);
            return dCmd.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally
        {
            dCmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }
}

Business Access Layer (BAL) Code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for EmployeeBAL
/// </summary>
public class EmployeeBAL
{
    public EmployeeBAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public int Insert(string E_name, string E_address, int E_age)
    {
        EmployeeDAL eDAL = new EmployeeDAL();
        try
        {
            return eDAL.Insert(E_name, E_address, E_age);
        }
        catch
        {
            throw;
        }
        finally
        {
            eDAL = null;
        }
    }
}


Code for .cs file

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Lets validate the page first
        if (!Page.IsValid)
            return;
        int intResult = 0;
        EmployeeBAL eBAL = new EmployeeBAL();
       // Instantiate the object we have to deal with
        string name = txtname.Text;
        string address = txtadd.Text;
        int age = Int32.Parse(txtage.Text);
        try
        {
            intResult = eBAL.Insert(name, address, age);
            if (intResult > 0)
                lblmsg.Text = "New record inserted successfully.";
            else
                lblmsg.Text = "FirstName [<b>" + txtname.Text + "</b>]alredy exists, try another name";
        }
        catch (Exception ee)
        {
            lblmsg.Text = ee.Message.ToString();
        }
        finally
        {
            eBAL = null;
        }        
    }
}

Code for Web.Config File

<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\v2.x\Config 
-->
<configuration>
 
	<appSettings/>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=dell-pc;Initial Catalog=JavaTest;User Id=sa;Password=niitbpld95"/>
  </connectionStrings>
  
    
  
	<system.web>
		<!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
		<compilation debug="true"/>
		<!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
		<authentication mode="Windows"/>
		<!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
	</system.web>
</configuration>

 


Presentation Layer


1


Database output:


2


Technorati Tags:

Comments

  1. use EDM in this and u need only bussiness logic and Presentation and used LINQ also...
    try this is to simple than preparing data layer
    this is more prefered way in companies

    ReplyDelete
  2. Hello, all the time i used to check web site posts here early in the
    daylight, since i like to gain knowledge of more and more.


    Here is my web site; Solve Captchas

    ReplyDelete

Post a Comment

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

C++ Program to define a Class BOOK and accessing member function using its object.

  #include<iostream.h> #include<stdio.h> #include<conio.h> class BOOK { int BOOKNO; char BOOKTITLE[20]; float PRICE; void TOTAL_COST( int N) { float tcost; tcost=PRICE*N; cout<<tcost; } public : void INPUT() { cout<<" Enter Book Number "; cin>>BOOKNO; cout<<" Enter Book Title "; gets(BOOKTITLE); cout<<" Enter price per copy "; cin>>PRICE; } void PURCHASE() { int n; cout<<" Enter number of copies to purchase "; cin>>n; cout<<" Total cost is "; TOTAL_COST(n); } }; void main() { BOOK obj; obj.INPUT(); obj.PURCHASE(); getch(); }

Addition of Two Number using servlet and jsp

index.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > < title > JSP Page </ title > </ head > < body bgcolor ="yellow" > < center >< h1 > Addition of Two Number </ h1 ></ center > < form action =" additionservlet " method ="post" > < table border ="0" width ="100" align ="center" > < tr > < td > First Number </ td > < td >< input type ="text" name ="txtnum1" value ="" /></ td > ...