Friday, November 14, 2014

Simple 3-tier Coding Pattern

Default.asp 


<%@ 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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table style="width:100%;">
            <tr>
                <td>
                    &nbsp;ID :-&nbsp;</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Name :-</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    City :-</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Salary :-</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </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 System.Data;
public partial class _Default : System.Web.UI.Page
{
    BAL b = new BAL();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
        ds.Tables.Add(b.GetData("EmpMst"));
        GridView1.DataSource = ds.Tables["EmpMst"];
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        b.Id = Convert.ToInt16(TextBox1.Text);
        b.Name = TextBox2.Text;
        b.City = TextBox3.Text;
        b.Salary = Convert.ToInt16(TextBox4.Text);
        b.SetData();
        bind();
    }
}



BAL.CS


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;


/// <summary>
/// Summary description for BAL
/// </summary>
public class BAL
{
    DAL d = new DAL();
public BAL()
{
//
// TODO: Add constructor logic here
//
}
    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private string city;

    public string City
    {
        get { return city; }
        set { city = value; }
    }
    private int salary;

    public int Salary
    {
        get { return salary; }
        set { salary = value; }
    }
    public void SetData()
    {
        d.SetEmpData(id, name, city, salary);
    }
    public DataTable GetData(string strtable)
    {
        return d.GetEmpData(strtable);
    }
}

DAL.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["strcnn"].ToString());
    public DAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public void SetEmpData(int id, string name, string city, int salary)
    {
        cnn.Open();
        SqlCommand cmd = new SqlCommand("dbo.insUp", cnn);
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@city", city);
        cmd.Parameters.AddWithValue("@salary", salary);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        cnn.Close();

    }
    public DataTable  GetEmpData(string strtable)
    {
        DataTable dt = new DataTable(strtable);
        cnn.Open();
        SqlCommand cmd = new SqlCommand("dbo.SelectData", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        cnn.Close();
        return dt;
    }
}


No comments:

Post a Comment

Demo for Repository Pattern in ASP.Net

----------------------------------------------------------- ----------------------------------------------------------- Repository Projec...