1.Business Logic Layer-(BLL)
2.Data Access Layer(DAL)
3.Presentation Layer.
Step-1 : Create a simple table Dept
Step4:-Add following code into businessObjects.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SimpleNtier
{
public class Dept
{
public Dept(int Deptno, string Dname, string Loc)
{
this.Deptno = Deptno;
this.Dname = Dname;
this.Loc = Loc;
}
private int _Deptno;
private string _Dname;
private string _Loc;
public int Deptno
{
get
{
return _Deptno;
}
set
{
_Deptno = value;
}
}
public string Dname
{
get
{
return _Dname;
}
set
{
_Dname = value;
}
}
public string Loc
{
get
{
return _Loc;
}
set
{
_Loc = value;
}
}
}
}
This contains data objects for all the fields of the table.
Step4:- Add following code to data access.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace SimpleNtier
{
public class DataAccess
{
public static void Add_dept(Dept b, ActionType action)
{
string sp_add = "spManageDept";
SqlParameter pr_Action = new SqlParameter("@ActionType", SqlDbType.SmallInt);
SqlParameter pr_Deptno = new SqlParameter("@Deptno", SqlDbType.Int);
SqlParameter pr_Dname = new SqlParameter("@Dname", SqlDbType.VarChar);
SqlParameter pr_Loc = new SqlParameter("Loc", SqlDbType.NVarChar);
pr_Action.Value = action;
if (action == ActionType.Add)
pr_Deptno.Direction = ParameterDirection.Input;
else
pr_Deptno.Direction = ParameterDirection.Output;
pr_Deptno.Value = b.Deptno;
if ((action == ActionType.Add) || (action == ActionType.Modify))
{
pr_Dname.Value = b.Dname;
pr_Loc.Value = b.Loc;
}
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.StoredProcedure, sp_add, pr_Action, pr_Deptno, pr_Dname, pr_Loc);
if (action == ActionType.Add)
pr_Deptno.Value = b.Deptno;
}
public static DataSet getDept()
{
string s_dt = "sp_GetDept";
SqlParameter pr_Deptnp = new SqlParameter("@Deptno", SqlDbType.Int);
pr_Deptnp.Value = -1;
DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.ConnectionString, CommandType.StoredProcedure, s_dt, pr_Deptnp);
ds.Tables[0].TableName = "Dept";
return ds;
}
public static Dept getDeptById(int Deptno)
{
string s_dt = "sp_GetDept";
SqlParameter pr_Deptnp = new SqlParameter("@Deptno", SqlDbType.Int);
pr_Deptnp.Value = Deptno;
SqlDataReader dr=SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.StoredProcedure, s_dt, pr_Deptnp);
Dept objDept=null;
if(dr.HasRows)
{
dr.Read();
objDept=new Dept((int) dr["Deptno"],(string) dr["Dname"],(string) dr["Loc"]);
}
dr.Close();
return objDept;
}
}
}
step 5:- Add following code to Item.aspx and Item.aspx.cs pages
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace SimpleNtier
{
public partial class Item1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Loadgrid();
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (txtDeptno.Text != null)
{
Dept b = new Dept(Convert.ToInt32(txtDeptno.Text), txtDname.Text, txtLoc.Text);
if (DataAccess.getDeptById(Convert.ToInt32(txtDeptno.Text)) != null)
{
DataAccess.Add_dept(b, ActionType.Modify);
Response.Write("<script>alert('Successfully Updated')</script>");
Loadgrid();
}
else if (DataAccess.getDeptById(Convert.ToInt32(txtDeptno.Text)) == null)
{
DataAccess.Add_dept(b, ActionType.Add);
Response.Write("<script>alert('Successfully Added')</script>");
Loadgrid();
}
}
}
private void Loadgrid()
{
DataSet ds = DataAccess.getDept();
grdDept.DataSource = ds;
grdDept.DataBind();
}
}
}
Item.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Item1.aspx.cs" Inherits="SimpleNtier.Item1" %>
<!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>
<style type="text/css">
.style1 {
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlDept" runat="server" Width="100%">
<table>
<tr>
<td></td>
<td><asp:Label ID="lblDeptno" runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtDeptno" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtDeptno" ErrorMessage="*" SetFocusOnError="True"
ValidationGroup="Item"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td></td>
<td><asp:Label ID="lblDname" runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtDname" runat="server"></asp:TextBox></td>
<td></td>
</tr>
<tr>
<td></td>
<td><asp:Label ID="lblLoc" runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtLoc" runat="server"></asp:TextBox></td>
<td></td>
</tr>
<tr>
<td></td>
<td><asp:button ID="btnSave" runat="server" onclick="btnSave_Click"
ValidationGroup="Item"></asp:button></td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="pnlGrid" runat="server">
<asp:GridView ID="grdDept" runat="server">
</asp:GridView>
</asp:Panel>
</div>
</form>
</body>
</html>



No comments:
Post a Comment