3-Tier using DAL, BAL in C#
/////*.aspx/////
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td style=”width: 100px”>
<asp:Label ID=”Label1″ runat=”server” Text=”Name”></asp:Label></td>
<td style=”width: 169px”>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
<asp:Label ID=”Label2″ runat=”server” Text=”Phone No”></asp:Label></td>
<td style=”width: 169px”>
<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
<asp:Label ID=”Label3″ runat=”server” Text=”Age”></asp:Label></td>
<td style=”width: 169px”>
<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
</td>
<td style=”width: 169px”>
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Insert” /></td>
<td style=”width: 100px”>
<asp:Button ID=”Button2″ runat=”server” OnClick=”Button2_Click” Text=”View” /></td>
</tr>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td style=”width: 100px”>
<asp:Label ID=”Label1″ runat=”server” Text=”Name”></asp:Label></td>
<td style=”width: 169px”>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
<asp:Label ID=”Label2″ runat=”server” Text=”Phone No”></asp:Label></td>
<td style=”width: 169px”>
<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
<asp:Label ID=”Label3″ runat=”server” Text=”Age”></asp:Label></td>
<td style=”width: 169px”>
<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
</td>
<td style=”width: 169px”>
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Insert” /></td>
<td style=”width: 100px”>
<asp:Button ID=”Button2″ runat=”server” OnClick=”Button2_Click” Text=”View” /></td>
</tr>
<tr>
<td style=”width: 100px”>
</td>
<td style=”width: 169px”>
<asp:Button ID=”Button3″ runat=”server” OnClick=”Button3_Click” Text=”Delete” /></td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px; height: 256px”>
</td>
<td style=”width: 169px; height: 256px”>
<asp:GridView ID=”GridView1″ runat=”server”>
<Columns>
<asp:BoundField DataField=”Id” HeaderText=”Id” />
<asp:BoundField />
<asp:BoundField />
<asp:TemplateField></asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:GridView ID=”GridView2″ runat=”server” AutoGenerateColumns=”False” OnRowEditing=”GridView2_RowEditing”
OnRowUpdating=”GridView2_RowUpdating” OnRowCancelingEdit=”GridView2_RowCancelingEdit”
OnRowDeleting=”GridView2_RowDeleting”>
<Columns>
<asp:CommandField ShowDeleteButton=”True” ShowEditButton=”True” />
<asp:BoundField HeaderText=”Id” DataField=”Id” ReadOnly=”true” />
<asp:BoundField HeaderText=”Name” DataField=”Name” />
<asp:BoundField HeaderText=”Age” DataField=”Age” />
</Columns>
</asp:GridView>
</td>
<td style=”width: 100px; height: 256px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
</td>
<td style=”width: 169px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
</div>
</form>
</body>
<td style=”width: 100px”>
</td>
<td style=”width: 169px”>
<asp:Button ID=”Button3″ runat=”server” OnClick=”Button3_Click” Text=”Delete” /></td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px; height: 256px”>
</td>
<td style=”width: 169px; height: 256px”>
<asp:GridView ID=”GridView1″ runat=”server”>
<Columns>
<asp:BoundField DataField=”Id” HeaderText=”Id” />
<asp:BoundField />
<asp:BoundField />
<asp:TemplateField></asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:GridView ID=”GridView2″ runat=”server” AutoGenerateColumns=”False” OnRowEditing=”GridView2_RowEditing”
OnRowUpdating=”GridView2_RowUpdating” OnRowCancelingEdit=”GridView2_RowCancelingEdit”
OnRowDeleting=”GridView2_RowDeleting”>
<Columns>
<asp:CommandField ShowDeleteButton=”True” ShowEditButton=”True” />
<asp:BoundField HeaderText=”Id” DataField=”Id” ReadOnly=”true” />
<asp:BoundField HeaderText=”Name” DataField=”Name” />
<asp:BoundField HeaderText=”Age” DataField=”Age” />
</Columns>
</asp:GridView>
</td>
<td style=”width: 100px; height: 256px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
</td>
<td style=”width: 169px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
</div>
</form>
</body>
/////*.aspx.cs ////
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
{
Bal bal1 = new Bal();
protected void Page_Load(object sender, EventArgs e)
{
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Bal bal1 = new Bal();
bal1.name = TextBox1.Text;
bal1.Phone_no = Convert.ToDecimal(TextBox2.Text);
bal1.Age = Convert.ToDecimal(TextBox3.Text);
int result = Bal.Insert_bal(bal1);
}
{
Bal bal1 = new Bal();
bal1.name = TextBox1.Text;
bal1.Phone_no = Convert.ToDecimal(TextBox2.Text);
bal1.Age = Convert.ToDecimal(TextBox3.Text);
int result = Bal.Insert_bal(bal1);
}
protected void Button2_Click(object sender, EventArgs e)
{
bind_grid();
}
{
bind_grid();
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
bind_grid();
}
{
GridView2.EditIndex = e.NewEditIndex;
bind_grid();
}
private void bind_grid()
{
DataSet ds = new DataSet();
ds = Bal.view();
GridView1.DataSource = ds;
GridView1.DataBind();
GridView2.DataSource = ds;
GridView2.DataBind();
}
{
DataSet ds = new DataSet();
ds = Bal.view();
GridView1.DataSource = ds;
GridView1.DataBind();
GridView2.DataSource = ds;
GridView2.DataBind();
}
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bal1.Id=Convert.ToDecimal(GridView2.Rows[e.RowIndex].Cells[1].Text);
{
bal1.Id=Convert.ToDecimal(GridView2.Rows[e.RowIndex].Cells[1].Text);
TextBox txt1 = new TextBox();
txt1 = (TextBox)GridView2.Rows[e.RowIndex].Cells[2].Controls[0];
bal1.name = txt1.Text;
txt1 = (TextBox)GridView2.Rows[e.RowIndex].Cells[2].Controls[0];
bal1.name = txt1.Text;
TextBox txt2 = new TextBox();
txt2 = (TextBox)GridView2.Rows[e.RowIndex].Cells[3].Controls[0];
bal1.Age = Convert.ToDecimal(txt2.Text);
txt2 = (TextBox)GridView2.Rows[e.RowIndex].Cells[3].Controls[0];
bal1.Age = Convert.ToDecimal(txt2.Text);
int re=Bal.update(bal1);
GridView2.EditIndex = -1;
bind_grid();
GridView2.EditIndex = -1;
bind_grid();
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex = -1;
bind_grid();
}
{
GridView2.EditIndex = -1;
bind_grid();
}
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
bal1.Id=Convert.ToDecimal(GridView2.Rows[e.RowIndex].Cells[1].Text);
{
bal1.Id=Convert.ToDecimal(GridView2.Rows[e.RowIndex].Cells[1].Text);
int re = Bal.delete(Convert.ToInt32(bal1.Id));
bind_grid();
}
protected void Button3_Click(object sender, EventArgs e)
{
bal1.Id=Convert.ToInt32(TextBox1.Text);
int re = Bal.delete(Convert.ToInt32(bal1.Id));
}
}
{
bal1.Id=Convert.ToInt32(TextBox1.Text);
int re = Bal.delete(Convert.ToInt32(bal1.Id));
}
}
/////web.config/////
<add name=”con” connectionString=”Data Source=192.168.3.253;Initial Catalog=temp1;User ID=sa;password=Sql2005″ providerName=”System.Data.SqlClient”/>
/////DAL.cs/////
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Dal
/// </summary>
public class Dal
{
public static string connect ;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Dal
/// </summary>
public class Dal
{
public static string connect ;
public Dal()
{
//
// TODO: Add constructor logic here
//
}
{
//
// TODO: Add constructor logic here
//
}
public static int Insert(Bal obj_insert)
{
int result;
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlCommand cmd = new SqlCommand(“sp_insert_Emp_Detail”, con);
cmd.CommandType = CommandType.StoredProcedure;
{
int result;
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlCommand cmd = new SqlCommand(“sp_insert_Emp_Detail”, con);
cmd.CommandType = CommandType.StoredProcedure;
// cmd.CommandText = “sp_insert_Emp_Detail”;
con.Open();
cmd.Parameters.AddWithValue(“Name”, obj_insert.name);
cmd.Parameters.AddWithValue(“Phone_no”, obj_insert.Phone_no);
cmd.Parameters.AddWithValue(“Age”, obj_insert.Age);
cmd.Parameters.AddWithValue(“Phone_no”, obj_insert.Phone_no);
cmd.Parameters.AddWithValue(“Age”, obj_insert.Age);
result= cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
return result;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
public static DataSet view()
{
DataSet ds=new DataSet();
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
//SqlCommand cmd = new SqlCommand(“View_sp”, con);
//cmd.CommandType = CommandType.StoredProcedure;
{
DataSet ds=new DataSet();
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
//SqlCommand cmd = new SqlCommand(“View_sp”, con);
//cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(“View_sp”, con);
con.Open();
//DataSet ds=new DataSet();
adp.Fill(ds);
con.Close();
}
catch(Exception ex)
{
throw ex;
}
return ds;
}
//DataSet ds=new DataSet();
adp.Fill(ds);
con.Close();
}
catch(Exception ex)
{
throw ex;
}
return ds;
}
public static int update(Bal bal1)
{
int result;
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlCommand cmd = new SqlCommand(“Update_sp”, con);
cmd.CommandType = CommandType.StoredProcedure;
{
int result;
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlCommand cmd = new SqlCommand(“Update_sp”, con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue(“Id”, bal1.Id);
cmd.Parameters.AddWithValue(“Name”, bal1.name);
cmd.Parameters.AddWithValue(“Age”, bal1.Age);
cmd.Parameters.AddWithValue(“Id”, bal1.Id);
cmd.Parameters.AddWithValue(“Name”, bal1.name);
cmd.Parameters.AddWithValue(“Age”, bal1.Age);
result = cmd.ExecuteNonQuery();
con.Close();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
return result;
}
public static int delete(int Id)
{
int result;
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlCommand cmd = new SqlCommand(“Delete_sp”, con);
cmd.CommandType = CommandType.StoredProcedure;
catch (Exception ex)
{
throw ex;
}
return result;
}
public static int delete(int Id)
{
int result;
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlCommand cmd = new SqlCommand(“Delete_sp”, con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue(“Id”, Id);
result=cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
throw ex;
cmd.Parameters.AddWithValue(“Id”, Id);
result=cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
return result;
}
}
return result;
}
}
/////BAL.cs/////
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for Bal
/// </summary>
public class Bal
{
public Bal()
{
//
// TODO: Add constructor logic here
//
}
private Decimal _Id;
public Decimal Id
{
get { return _Id; }
set { _Id = value; }
}
private String _name;
public String name
{
get { return _name; }
set { _name = value; }
}
/// Summary description for Bal
/// </summary>
public class Bal
{
public Bal()
{
//
// TODO: Add constructor logic here
//
}
private Decimal _Id;
public Decimal Id
{
get { return _Id; }
set { _Id = value; }
}
private String _name;
public String name
{
get { return _name; }
set { _name = value; }
}
private Decimal _Phone_no;
public Decimal Phone_no
{
get { return _Phone_no; }
set { _Phone_no = value; }
}
private Decimal _Age;
public Decimal Age
{
get { return _Age; }
set { _Age = value; }
}
public Decimal Phone_no
{
get { return _Phone_no; }
set { _Phone_no = value; }
}
private Decimal _Age;
public Decimal Age
{
get { return _Age; }
set { _Age = value; }
}
public static int Insert_bal(Bal bal1)
{
try
{
{
try
{
return Dal.Insert(bal1);
}
catch (Exception ex)
{
throw ex;
}
}
public static DataSet view()
{
try
{
return Dal.view();
}
catch (Exception ex)
{
throw ex;
}
}
public static int update(Bal bal1)
{
try
{
return Dal.update(bal1);
}
catch (Exception ex)
{
throw ex;
}
}
public static int delete(int Id)
{
try
{
return Dal.delete(Id);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public static DataSet view()
{
try
{
return Dal.view();
}
catch (Exception ex)
{
throw ex;
}
}
public static int update(Bal bal1)
{
try
{
return Dal.update(bal1);
}
catch (Exception ex)
{
throw ex;
}
}
public static int delete(int Id)
{
try
{
return Dal.delete(Id);
}
catch (Exception ex)
{
throw ex;
}
}
}
No comments:
Post a Comment