Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
3 layer architecture example program in asp.net
6606 Views
Introduction 
In this program you will learn about simple 3 layer architecture

Note: BLL - Business logic layer
          DAL - Data accessing layer
          BO - Business objects.
DALINSERT.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using BO;

namespace DAL
{
    public class DALINSERT
    {
        public void dalins(PROPERTIES pro)
        {
            string str = @"Data Source=LAKSHANYA\SQLEXPRESS; Initial Catalog=master;Integrated Security=True";
            SqlConnection conn = new SqlConnection(str);//Making connection to DB.
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO student VALUES('"+pro._userName+"','"+pro._gender+"','"+pro._address+"','"+pro._mark+"','"+pro._status+"')",conn);
                rdr = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
                if (conn != null)
                    conn.Close();
            }
        }
    }
}
Web page preview 
After inserting value you can see the values in DB. 
Conclusion 
This program helps you to understand about basic structure of 3 layer architecture program.
Thanks for read it.
Functionality 
In this pogram, I am talking the four fields value in the web page.And according to the mark value i am storing pass(more than 40) or fail(less than 40) status in the database.After the status stored the page shown the Success in another page.
Sql Queries to create table
create table student(
username nvarchar(20),
gender varchar(10),
address varchar(20),
mark varchar(20),
status varchar(20))
Note 
Create the 3 class library for BLL,BO,DAL

In BLL - Used to write logic.For example in this program i have write the logic for find the status pass or fail.
In BO -Used to write the properties of the program.
In DAL - Used to write code to access the database.In this program after getting all the fields value and status i insert the value to the database.
Solution Explorer 

Don't forget to refer the BO class library in DAL and BLL and DAL in BLL.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 266px;
       }
        .auto-style3 {
            height: 23px;
            text-align: justify;
        }
        .auto-style4 {
            width: 118px;
        }
        .auto-style5 {
            width: 118px;
            height: 23px;
        }
        .auto-style6 {
            width: 266px;
            height: 23px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>ENTER THE FIELDS TO SEE THE STATUS</h1>
        </div>
    <div>
        <table class="auto-style1">
            <tr>
                <td class="auto-style4">User Name</td>
                <td class="auto-style2">
                    <asp:TextBox ID="Uname" runat="server" Width="167px"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">Gender</td>
                <td class="auto-style6">
                    <asp:RadioButtonList ID="Gender" runat="server">
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td class="auto-style3"></td>
            </tr>
            <tr>
                <td class="auto-style4">Address</td>
                <td class="auto-style2">
                    <asp:TextBox ID="Address" runat="server" TextMode="MultiLine"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style4">Mark</td>
                <td class="auto-style2">
                    <asp:TextBox ID="Mark" runat="server" Width="170px"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style3" colspan="3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="Button1" runat="server" Text="submit" onclick="Button1_Click" />
                </td>
            </tr>
        </table>
        <br />
    </div>
    </form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using BLL;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        PROPERTIES properties = new PROPERTIES();//object created for properties class
        INSERT insert = new INSERT();//object created for insert class
        properties._userName = Uname.Text;
        properties._gender = Gender.SelectedValue;
        properties._address = Address.Text;
        properties._mark =Mark.Text;
        insert.status(properties);//passing value to BLL function via properties
        insert.ins(properties);//passing value to BLL function via properties
        Response.Redirect("success.aspx");//Finally redirect to success page.
    }
}
INSERT.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using DAL;

namespace BLL
{
    public class INSERT
    {
        DALINSERT dalinsert=new DALINSERT();//object of dal class

        public void status(PROPERTIES prop)//Function to find status
        {
            if (Convert.ToInt32(prop._mark) >= 40)
                prop._status="pass";
            else
                prop._status= "fail";
        }
        public void ins(PROPERTIES properties)//Function to send value to DAL
        {
            try
            {
                    dalinsert.dalins(properties);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
   }
}
PROPERTIES.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BO
{
    public class PROPERTIES
    {
        private string userName;
        private string gender;
        private string address;
        private string status;
        private string mark;

        public string _userName
        {
            get { return userName; }
            set { userName = value; }
        }
        public string _gender
        {
            get { return gender; }
            set { gender = value; }
        }
        public string _address
        {
            get { return address; }
            set { address = value; }
        }
        public string _mark
        {
            get { return mark; }
            set { mark = value; }
        }
        public string _status
        {
            get { return status; }
            set { status = value; }
        }
    }
}
Best Lessons of "Asp.net/Ado.net/C#.net(Dotnet)"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details