Three Tier Architecture

presentation

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



namespace example 

public partial class index : System.Web.UI.Page 

BAL bal = new BAL(); 



protected void Page_Load(object sender, EventArgs e) 

if (!Page.IsPostBack) 

RefreshData(); 





public void RefreshData() 

GridView1.DataSource = bal.GetData(); 
GridView1.DataBind(); 




protected void Button1_Click(object sender, EventArgs e) 

bool status = bal.insertdata(txtndame.Text,txtaddr.Text);
RefreshData(); 




protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 

int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Values["userid"].ToString());
bal.Delete(id); 
RefreshData(); 



protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Values["userid"].ToString());
TextBox txtname = GridView1.Rows[e.RowIndex].FindControl("txtname") as TextBox; 
TextBox txtaddr = GridView1.Rows[e.RowIndex].FindControl("txtaddr") as TextBox; 
bal.Update(txtname.Text, txtaddr.Text,id); 
GridView1.EditIndex = -1; 
RefreshData(); 




protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 

GridView1.EditIndex = e.NewEditIndex; 
RefreshData(); 





protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 

GridView1.EditIndex = -1; 
RefreshData(); 




} }


Data Access

public class DAL 

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
SqlCommand cmd; 


public bool insertdata(sting name, string address) 

bool status = false; 
cmd = new SqlCommand("sp_insert", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.AddWithValue("@Name",name);
cmd.Parameters.AddWithValue("@Address", address); 
con.Open(); 
int res = cmd.ExecuteNonQuery(); 
con.Close(); 
if(res>0) 

status = true; 

return status; 



public DataTable GetData() 

cmd = new SqlCommand("sp_GetDall", con); 
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd); 
DataTable dt = new DataTable(); 
da.Fill(dt); 
return dt; 



public void updatedata(string name, string address, int id) 

cmd = new SqlCommand("sp_update", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.AddWithValue("@Name", name); 
cmd.Parameters.AddWithValue("@Address",address);
cmd.Parameters.AddWithValue("@userid", id); 
con.Open(); 
int i = cmd.ExecuteNonQuery(); 
con.Close(); 



public void deletedata(int id) 

cmd = new SqlCommand("sp_delete", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.AddWithValue("@userid", id); 
con.Open(); 
int i = cmd.ExecuteNonQuery(); 
con.Close(); 




Business 
using BusinessEntitierLayer; 
using DataAccessLayer; 
namespace BusinessAccessLayer 



public class BAL 

DAL da = new DAL(); 
public bool insertdata(sting name, string address) 

bool status = false; 
da.insertdata(name,address); 
return status; 



public DataTable GetData() 

return da.GetData(); 



public void deletedata(int id) 

da.Delete(id); 



public void updatedata(string name,string address,int id) 

da.Update(name,email,address, mobile, id); 


}

Comments

Popular posts from this blog

loop example

View vs Partial View MVC