Saturday, 31 August 2013

Working with 3-Tier Architecture..



3-Tier Architecture:

            3-Tier architecture generally contains UI or Presentation Layer, Business Access Layer (BAL) or Business Logic Layer and Data Access Layer (DAL).
 
The 3 Layers are
  1.      Presentation Layer
  2.      Business Layer
  3.      Data Access Layer
 
1) Presentation Layer:
 
In this layer we perform all presentation related information or Graphical User Interface (GUI). The logic or calculations and other code should be return in this layer.
 
2) Business Layer:
 
Most of the logics are return in this layer application all other layers use this layer for providing inputs to it and also to get the outputs from it.
 
3) Data Access Layer:
 
In this layer we should be perform Data Source related operations. For communicate to Database in our application we use this layer here we perform all operations like insertion, deletion, updating and selection.

How to prepare 3-Tier Application


1) Open Microsoft Visual Studio and then choose File in top menu-bar and then choose new option then choose website and then give a name for that.



2) Prepare all layers into our project in step by step. First we prepare Data Layer class for that purpose we prepare one new folder name as” App_Code”.
3) Right Click on your project then choose option called New Folder.



4) Above screen will give brief description about that.



5) Now, right click on your App_Code folder then choose Add New Item



6) Then Create a class file name as DataLayer.cs.



7) Write a below lines of code in your datalayer class.



using System;u
sing System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;///

/// Summary description for DataLayer
///
public class DataLayer
{
    
public DataLayer()
    
{
      
//
       // TODO: Add constructor logic here
       //
    
}
    
public DataSet Get_Data(string SqlStmt)
    
{
        SqlConnection con
= new SqlConnection("DataBase=ENGSOFT;User id=sa;Password=P@ssword9");
        
DataSet ds = new DataSet();
        
SqlDataAdapter da = new SqlDataAdapter(SqlStmt, con);
        
da.Fill(ds);
        
return ds;
    
}
}
8) Then add one more class called as BLayer.cs under App_Code folder






9) Wrote below lines of code under Business Layer class.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
///
/// Summary description for BLayer
///
public class BLayer
{
    
public BLayer()
    
{
      
//
       // TODO: Add constructor logic here
       //
    
}
    
public DataSet Get_Data()
    
{
        DataLayer dl
= new DataLayer();
        
return dl.Get_Data("select EmpNo,Ename,Job,Mgr,Sal,Comm from Emp");
    
}
}


10) After successfully prepare DataLayer and Business Layer classes then create Application Layer
 for that right click on your project then choose newitem and then choose WebForm.





11) Design a page like below, drag and one button control and one gridview control.


 


Source Code:




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="3TierApp.aspx.cs" Inherits="_3TierApp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

<html xmlns=http://www.w3.org/1999/xhtml>
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            font-size: x-large;
            color: #003300;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
        <strong><em>
        <br class="style1" />
        </em><span class="style1"><em>3Tier Application</em></span></strong><br />
        <br />
    
        <asp:Button ID="btnFetch" runat="server" Text="Fetch" onclick="btnFetch_Click" />
        <br />
        <br />
        <asp:GridView ID="GV3Tier" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>
    </div>
    <br />
    <br />
    
    </form>
</body>
</html>






12) Wrote below lines of under button click event.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _3TierApp : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }
    
protected void btnFetch_Click(object sender, EventArgs e)
    
{
        BLayer obj
= new BLayer();
        
GV3Tier.DataSource = obj.Get_Data();
        
GV3Tier.DataBind();
    
}
}

Out Put