3 Tier Samples
3 Tier Samples
WebControls; using System.Data; using Employee_Details.BLL; namespace Employee_Details.PL { public partial class Employee : System.Web.UI.Page { BL_EmpMethods bll = new BL_EmpMethods(); BLL_Emp bl = new BLL_Emp(); BLL_Department dl = new BLL_Department(); BL_DepMethods dep = new BL_DepMethods(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Emp_Grid(); dep_dll(); } } private void dep_dll() { dll_depname.DataSource = dep.depart_grid(); dll_depname.DataTextField = "DepartmentName"; dll_depname.DataValueField = "DepartmentId"; dll_depname.DataBind(); }
protected void btn_Submit_Click(object sender, EventArgs e) { bl.EmployeeId = Convert.ToInt32(txt_EmployeeID.Text); bl.EmployeeName = txt_Empname.Text; bl.Salary = Convert.ToInt32(txt_salary.Text); dl.DepartmentId = Convert.ToInt32(dll_depname.Text); lblMessage.Text = BL_EmpMethods.Employee_insert(bl,dl); Emp_Grid(); clr(); } private void clr() { txt_EmployeeID.Text = ""; txt_Empname.Text = ""; txt_salary.Text = ""; } public void Emp_Grid() { grd_Emp_Details.DataSource = bll.Employee_grid(); grd_Emp_Details.DataBind();
} protected void grd_Emp_Details_RowEditing(object sender, GridViewEditEve ntArgs e) { grd_Emp_Details.EditIndex = e.NewEditIndex; Emp_Grid(); } protected void grd_Emp_Details_RowCancelingEdit(object sender, GridViewC ancelEditEventArgs e) { grd_Emp_Details.EditIndex = -1; Emp_Grid(); } protected void grd_Emp_Details_RowCommand(object sender, GridViewCommand EventArgs e) { if (e.CommandName == "SELECT") { int Employeeid = int.Parse(e.CommandArgument.ToString()); Session["EmployeeId"] = Employeeid; Response.Redirect("SelectEmployee.aspx"); } } protected void grd_Emp_Details_RowUpdating(object sender, GridViewUpdate EventArgs e) { Label lblempid = (Label)grd_Emp_Details.Rows[e.RowIndex].FindControl ("lbl_EmpID"); TextBox txtempname = (TextBox)grd_Emp_Details.Rows[e.RowIndex].FindC ontrol("txt_empname"); DropDownList dllDepname = (DropDownList)grd_Emp_Details.Rows[e.RowIn dex].FindControl("dll_department"); TextBox txtsalary = (TextBox)grd_Emp_Details.Rows[e.RowIndex].FindCo ntrol("txt_salary"); bl.EmployeeId = Convert.ToInt32(lblempid.Text); bl.EmployeeName = txtempname.Text; bl.Salary = Convert.ToInt32(txtsalary.Text); dl.DepartmentId = Convert.ToInt32(dllDepname.Text); lblMessage.Text = BL_EmpMethods.Employee_update(bl, dl); grd_Emp_Details.EditIndex = -1; Emp_Grid(); } protected void grd_Emp_Details_RowDataBound(object sender, GridViewRowEv entArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (grd_Emp_Details.EditIndex == e.Row.RowIndex) { DropDownList ddl = (DropDownList)e.Row.FindControl("dll_depa rtment"); ddl.DataSource = dep.depart_grid();
ddl.DataTextField = "DepartmentName"; ddl.DataValueField = "Departmentid"; ddl.DataBind(); ddl.ClearSelection(); ddl.Items.FindByText(grd_Emp_Details.DataKeys[e.Row.RowIndex ]["DepartmentName"].ToString()).Selected = true; } } } protected void grd_Emp_Details_RowDeleting(object sender, GridViewDelete EventArgs e) { Label lblempid = (Label)grd_Emp_Details.Rows[e.RowIndex].FindControl ("lbl_EmpID"); bl.EmployeeId = Convert.ToInt32(lblempid.Text); lblMessage.Text = BL_EmpMethods.Employee_Delete(bl); Emp_Grid(); } protected void grd_Emp_Details_PageIndexChanging(object sender, GridView PageEventArgs e) { grd_Emp_Details.PageIndex = e.NewPageIndex; Emp_Grid(); } } } ----------------------------------------------------------------------------------------Business Logic Combined with DAL(Data Access Layer) ----------------------------------------------------------------------------------------using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using Employee_Details.DAL; namespace Employee_Details.BLL { public class BL_EmpMethods { public static string Employee_insert(BLL_Emp balobj,BLL_Department blobj ) { DAL_Emp Dal = new DAL_Emp(); return Dal.Dal_Employee_Insert(balobj,blobj); } public DataTable Employee_grid() { DAL_Emp Dal = new DAL_Emp(); return Dal.Dal_empgrid(); } public static string Employee_update(BLL_Emp balobj, BLL_Department blob j) {
DAL_Emp Dal = new DAL_Emp(); return Dal.Emp_Update(balobj,blobj); } public static string Employee_Delete(BLL_Emp balobj) { DAL_Emp Dal = new DAL_Emp(); return Dal.Dal_Emp_Delete(balobj); } public List<BE> getrecord(int EmpID) { DAL_Emp EmpDAL = new DAL_Emp(); return EmpDAL.getemployeerecord(EmpID); } } } --------------------------------------------------------------------------------------------------------------------------------------------------------------------------DAL --EMP ----------using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; using Employee_Details.BLL; using Employee_Details.PL; namespace Employee_Details.DAL { public class DAL_Emp { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStr ings["DB_Connection"].ToString()); SqlCommand cmd; SqlDataAdapter da; DataTable dt = new DataTable(); public string Dal_Employee_Insert(BLL_Emp bllobj,BLL_Department blobj) { try { cmd = new SqlCommand("Sp_insert_Employee", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Employee_id", bllobj.EmployeeId); cmd.Parameters.AddWithValue("@Employee_Name", bllobj.EmployeeNam e); cmd.Parameters.AddWithValue("@Department_id", blobj.DepartmentId ); cmd.Parameters.AddWithValue("@salary", bllobj.Salary); con.Open(); return cmd.ExecuteScalar().ToString();
} catch (Exception) { throw; } finally { con.Close(); } } public string Emp_Update(BLL_Emp beobj,BLL_Department blobj) { try { cmd = new SqlCommand("Sp_Update_Details", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Employee_Id", beobj.EmployeeId); cmd.Parameters.AddWithValue("@Employee_Name", beobj.EmployeeName ); cmd.Parameters.AddWithValue("@Department_Id", blobj.DepartmentId ); cmd.Parameters.AddWithValue("@salary", beobj.Salary); con.Open(); return cmd.ExecuteScalar().ToString(); } catch (Exception ee) { return ee.Message; } finally { con.Close(); } } public string Dal_Emp_Delete(BLL_Emp bllobj) { try { cmd = new SqlCommand("Sp_Delete_Employee", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Employee_Id", bllobj.EmployeeId); con.Open(); return cmd.ExecuteScalar().ToString(); } catch(Exception e) { return e.Message; } finally { con.Close(); }
} public DataTable Dal_empgrid() { cmd = new SqlCommand("Sp_Select_Employee", con); cmd.CommandType = CommandType.StoredProcedure; da = new SqlDataAdapter(cmd); da.Fill(dt); return dt; } public List<BE> getemployeerecord(int EmpID) { List<BE> lstempinfo = new List<BE>(); try { con.Open(); cmd = new SqlCommand("Sp_EmployeeDetails", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Employee_Id", EmpID); da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); foreach (DataRow dr in dt.Rows) { BE empinfo = new BE(); BLL_Department depinfo = new BLL_Department(); empinfo.EmployeeId = int.Parse(dr["EmployeeId"].ToString()); empinfo.EmployeeName = dr["EmployeeName"].ToString(); empinfo.Salary = int.Parse(dr["salary"].ToString()); empinfo.DepartmentName = dr["DepartmentName"].ToString(); empinfo.Location = dr["Location"].ToString(); lstempinfo.Add(empinfo); } return lstempinfo; } catch { throw; } finally { con.Close(); } } } } ------------------------------DAL-Dept ------------------------------using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration;
using Employee_Details.BLL; using Employee_Details.PL; namespace Employee_Details.DAL { public class DAL_Dep { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStr ings["DB_Connection"].ToString()); SqlCommand cmd; SqlDataAdapter da; DataTable dt = new DataTable(); public string Dal_Department_Insert(BLL_Department blobj) { try { cmd = new SqlCommand("Sp_insert_Department", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Department_id", blobj.DepartmentId ); cmd.Parameters.AddWithValue("@Department_Name", blobj.Department Name); cmd.Parameters.AddWithValue("@Location", blobj.Location); con.Open(); return cmd.ExecuteScalar().ToString(); } catch (Exception) { throw; } finally { con.Close(); } } public string Dep_Update(BLL_Department blobj) { try { cmd = new SqlCommand("Sp_Update_Department", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Department_Id", blobj.DepartmentId ); cmd.Parameters.AddWithValue("@Department_Name", blobj.Department Name); cmd.Parameters.AddWithValue("@Location", blobj.Location); con.Open(); return cmd.ExecuteScalar().ToString(); } catch (Exception ee) { return ee.Message; } finally { con.Close();
} } public string Dal_DepartmentDelete(BLL_Department beobj) { try { cmd = new SqlCommand("Sp_Delete_Department", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@DepartmentId",beobj.DepartmentId); con.Open(); return cmd.ExecuteScalar().ToString(); } catch (Exception e) { return e.Message; } } public DataTable Dal_departmentdetails() { cmd = new SqlCommand("Sp_Select_department", con); cmd.CommandType = CommandType.StoredProcedure; da = new SqlDataAdapter(cmd); da.Fill(dt); return dt; } } }