Thursday 13 February 2014

two tier architecture complete example


Two tier architecture in asp.net

Three 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#

Send mail in asp.net



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

if you want to develop website using Two 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 one class file (from right click on solution explorer) and set name
     1) bal.cs and
        (it is saved automatically in App_Code folder )

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

<div>
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/>        
<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 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 int id { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public string city { get; set; }




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

   

public void insert_data(bal bl)
    {
        cmd = new SqlCommand("insert into tbl values(@name,@age,@city)", cn);
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@age", age);
        cmd.Parameters.AddWithValue("@city", 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(bal bl)
    {
        cmd = new SqlCommand("update tbl set name=@name,age=@age,city=@city where id=@id", cn);

        cmd.Parameters.AddWithValue("id", id);
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("age", age);
        cmd.Parameters.AddWithValue("city", city);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();

    }
    public void delete_data(bal bl)
    {
        cmd = new SqlCommand("delete from tbl where id=@id", cn);
        cmd.Parameters.AddWithValue("@id", id);

        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
{
    bal bl = new bal();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            disp();
        }
    }
    public void disp()
    {
        Gridview1.DataSource = bl.disp_data();
        Gridview1.DataBind();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bl.name = TextBox2.Text;
        bl.age = Convert.ToInt32(TextBox3.Text);
        bl.city = TextBox4.Text;

        bl.insert_data(bl);
        disp();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        bl.id = Convert.ToInt32(TextBox1.Text);
        bl.name = TextBox2.Text;
        bl.age = Convert.ToInt32(TextBox3.Text);
        bl.city = TextBox4.Text;

        bl.update_data(bl);
        disp();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        bl.id = Convert.ToInt32(TextBox1.Text);

        bl.delete_data(bl);
        disp();
    }
}

No comments:

Post a Comment



Asp.net tutorials