Thursday 13 February 2014

three tier architecture complete example

Three tier architecture in Asp.net


Two tier architecture in Asp.net

One tier architecture in Asp.net

Simple login code in asp.net using c#

Log out code in asp.net with C#

How to create chat application in asp.net using c#



if you want to
-> insert record  using Three tier architecture in Asp.net OR
-> update record  using Three tier architecture in Asp.net OR
->delete record  using Three tier architecture in Asp.net OR
->display record  using Three tier architecture in Asp.net
then do following

if you want to develop website using Three tier architecture in Asp.net for data security that time do following

--> your database name:Student
         table name       :tbl
    field name :id,name,age,citz

-->first
    add Default.aspx page
    add two class file  and set name
     1) bal.cs and
     2)dal.cs    (it is saved automatically in App_Code folder )

--> next
    .aspx page(copy following source code in your default.aspx file)
------------------------------------------------------------------------------------------------

<div><h1>Three tier architecture in Asp.net </h1>
Id
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br/>        
Name           
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br/>           
Age           
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  <br/> 
City           
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>    <br/> <br/>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="add" />
<asp:Button ID="Button2" runat="server" Text="Edit" onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="delete" onclick="Button3_Click" />
<asp:Gridview Id="Gridview1" runat="server">
</asp:Gridview>
</div

--> next
    copy the following code and paste in your dal.cs file
------------------------------------------------------------------------------------------------


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

using System.Data;

public class dal
{
    public int id { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public string city { get; set; }

    bal bl = new bal();

    public dal()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public void insert_data1(dal dl)
    {
        bl.insert_data(dl);
    }
    public DataSet disp_data1()
    {
        return bl.disp_data();
    }
    public void update_data1(dal dl)
    {
        bl.update_data(dl);
    }
    public void delete_data1(int i)
    {
        bl.delete_data(i);
    }
}
--> next copy the following code and paste in your bal.cs file
------------------------------------------------------------------------------------------------


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

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class bal
{
        SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;

public bal()
{
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
}

public void insert_data(dal dl)
    {
        cmd = new SqlCommand("insert into tbl values(@name,@age,@city)", cn);
        cmd.Parameters.AddWithValue("@name", dl.name);
        cmd.Parameters.AddWithValue("@age", dl.age);
        cmd.Parameters.AddWithValue("@city", dl.city);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
    public DataSet disp_data()
    {
        cmd = new SqlCommand("select * from tbl", cn);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
    public void update_data(dal dl)
    {
        cmd = new SqlCommand("update tbl set name=@name,age=@age,city=@city where id=@id", cn);
        cmd.Parameters.AddWithValue("@id", dl.id);
        cmd.Parameters.AddWithValue("@name", dl.name);
        cmd.Parameters.AddWithValue("@age", dl.age);
        cmd.Parameters.AddWithValue("@city", dl.city);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
    public void delete_data(int i)
    {
        cmd = new SqlCommand("delete from tbl where id=@id", cn);
        cmd.Parameters.AddWithValue("@id", i);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
}

--> next write following connectionstring in your web.config file
------------------------------------------------------------------------------------------------


<configuration>
  <connectionStrings>
    <add name="conn" connectionString="Data Source=;Initial Catalog=student;Integrated Security=True"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>


--> next copy the following code and paste your default.aspx.cs file
------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    dal dl = new dal();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            disp();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        dl.name = TextBox2.Text;
        dl.age = Convert.ToInt32(TextBox3.Text);
        dl.city = TextBox4.Text;

        dl.insert_data1(dl);
        disp();
    }
    public void disp()
    {
        GridView1.DataSource = dl.disp_data1();
        GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        dl.id = Convert.ToInt32(TextBox1.Text);
        dl.name = TextBox2.Text;
        dl.age = Convert.ToInt32(TextBox3.Text);
        dl.city = TextBox4.Text;

        dl.update_data1(dl);
        disp();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox1.Text);
        dl.delete_data1(i);
        disp();
    }

No comments:

Post a Comment



Asp.net tutorials