Download Source
In this article you will learn how to fetch data, search data, insert into database and update database using ThreeTierArchitecture
Step 1: Create A Project And Give Name 3TierArchitecture
Step 2: After Creating Project Add Class Library Bussinesslayer and Datalinklayer
Step 3: Your Project Look This Type after adding all class Library
Step 4: Now we have to add references
(1). Right Click in Bussiness layer --> Add References --> Now Check Datalayer --> OK
(2). Next Go To 3tierarchitecture --> Right Click Select Add To References --> Check Both
Datalayer and Bussinesslayer --> OK
Step 5: Add Class in Bussinesslayer and Give name clsbussinesslayer and same step for datalayer
HTML Design
Code For Datalayer
Code for Bussinesslayer
Code For Home Page
Final Output
Download Source
In this article you will learn how to fetch data, search data, insert into database and update database using ThreeTierArchitecture
Step 1: Create A Project And Give Name 3TierArchitecture
Step 2: After Creating Project Add Class Library Bussinesslayer and Datalinklayer
Step 3: Your Project Look This Type after adding all class Library
Step 4: Now we have to add references
(1). Right Click in Bussiness layer --> Add References --> Now Check Datalayer --> OK
(2). Next Go To 3tierarchitecture --> Right Click Select Add To References --> Check Both
Datalayer and Bussinesslayer --> OK
Step 5: Add Class in Bussinesslayer and Give name clsbussinesslayer and same step for datalayer
HTML Design
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Button.css" rel="stylesheet" />
<style>
.textbox {
border: 1px solid #c4c4c4;
height: 25px;
width: 180px;
font-size: 13px;
padding: 4px 4px 4px 4px;
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
box-shadow: 0px 0px 8px #d9d9d9;
-moz-box-shadow: 0px 0px 8px #d9d9d9;
-webkit-box-shadow: 0px 0px 8px #d9d9d9;
}
.textbox:focus {
outline: none;
border: 1px solid #7bc1f7;
box-shadow: 0px 0px 8px #7bc1f7;
-moz-box-shadow: 0px 0px 8px #7bc1f7;
-webkit-box-shadow: 0px 0px 8px #7bc1f7;
}
</style>
</head>
<body style="background-color: #f7f5EE">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<div align="center">
<br />
<br />
<table>
<tr>
<td colspan="2"><b style="font-size: large; color: red">InsertUpdateDelete Using 3TierArchitecture</b><br />
<br />
</td>
<td></td>
</tr>
<tr>
<td><b>Id :</b></td>
<td>
<asp:DropDownList ID="ddlbind" runat="server" CssClass="textbox" AutoPostBack="true" Width="190px" Height="35px" OnSelectedIndexChanged="ddlbind_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td><b>Name :</b></td>
<td>
<input type="text" id="txtname" runat="server" class="textbox" /></td>
</tr>
<tr>
<td><b>Age :</b></td>
<td>
<input type="text" id="txtage" runat="server" class="textbox" /></td>
</tr>
<tr>
<td><b>Subject :</b></td>
<td>
<input type="text" id="txtsubject" runat="server" class="textbox" /></td>
</tr>
<tr>
<td colspan="2">
<br />
<input type="submit" id="btninsert" runat="server" value="Insert" onserverclick="btninsert_ServerClick" class="shiny-button" />
<input type="submit" id="btnupdate" runat="server" value="Update" class="shiny-button" onserverclick="btnupdate_ServerClick" />
<input type="submit" id="btndelete" runat="server" value="Delete" class="shiny-button" onserverclick="btndelete_ServerClick" />
</td>
<td></td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<div style="margin-left: 444px;">
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:GridView ID="grdbind" runat="server"
CssClass="gvwWhite" AlternatingRowStyle-CssClass="alt"
AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="6" ForeColor="Black" GridLines="Vertical">
<AlternatingRowStyle BackColor="White" CssClass="alt" />
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" HeaderStyle-Width="15%">
<HeaderStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="name" HeaderText="Name" HeaderStyle-Width="15%">
<HeaderStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="age" HeaderText="Age" HeaderStyle-Width="15%">
<HeaderStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="subject" HeaderText="Subject" HeaderStyle-Width="15%">
<HeaderStyle Width="15%" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Code For Datalayer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace Datalayer
{
public class clsdatalayer
{
string connection = "Data Source=Servername;Initial Catalog=Databasename;Integrated Security=True";
public void ExecuteInsertUpdateDelete(string sqlquery) //For InsertUpdateDelete
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand(sqlquery, con);
cmd.ExecuteNonQuery();
}
public DataSet ExecuteFetchdata(string sqlquery)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand(sqlquery, con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
public void Insertintodatabase(string name, string age, string subject) //For Insert
{
try
{
string sqlquery = "Insert into student(name,age,subject) values('" + name + "','" + age + "','" + subject + "')";
ExecuteInsertUpdateDelete(sqlquery);
}
catch (Exception ex)
{
}
}
public void Updateintodatabase(string id, string name, string age, string subject) //For Update
{
try
{
string sqlquery = "Update student set name='" + name + "',age='" + age + "',subject='" + subject + "' where id='" + id + "'";
ExecuteInsertUpdateDelete(sqlquery);
}
catch (Exception ex)
{
}
}
public void Deleteintodatabase(string id) //For Delete
{
try
{
string sqlquery = "Delete from student where id='" + id + "'";
ExecuteInsertUpdateDelete(sqlquery);
}
catch (Exception ex)
{
}
}
public DataSet filltextbox(string id) //Show Details in Textbox when Search Click
{
string sqlquery = "select * from student where id='" + id + "'";
DataSet ds = new DataSet();
ds = (DataSet)ExecuteFetchdata(sqlquery);
return ds;
}
public DataSet bindgridview() //Bind On Page Load
{
string sqlquery = "Select * from student";
DataSet ds = new DataSet();
ds = (DataSet)ExecuteFetchdata(sqlquery);
return ds;
}
}
}
Code for Bussinesslayer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Datalayer;
using System.Data;
namespace Bussinesslayer
{
public class clsbussiness
{
clsdatalayer da = new clsdatalayer();
public void Insertintodatabase(string name, string age, string subject)
{
da.Insertintodatabase(name, age, subject);
}
public void Updateintodatabase(string id, string name, string age, string subject)
{
da.Updateintodatabase(id, name, age, subject);
}
public void Deleteintodatabase(string id)
{
da.Deleteintodatabase(id);
}
public DataSet bindgridview()
{
return da.bindgridview();
}
public DataSet filltextbox(string id)
{
return da.filltextbox(id);
}
}
}
Code For Home Page
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 Bussinesslayer; //Add References
using Datalayer; //Add References
namespace _3tierarchitecture
{
public partial class Home_page : System.Web.UI.Page
{
clsbussiness bs = new clsbussiness();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindgrid();
}
}
public void bindgrid() //Bind Search Dropdownlist and Gridview On Page Load
{
grdbind.DataSource = bs.bindgridview();
grdbind.DataBind();
ddlbind.DataSource = bs.bindgridview();
ddlbind.DataTextField = "id";
ddlbind.DataValueField = "id";
ddlbind.DataBind();
ddlbind.Items.Insert(0, new ListItem("-------------- Select --------------", "0"));
}
public void clean()
{
txtage.Value = "";
txtname.Value = "";
txtsubject.Value = "";
}
protected void btninsert_ServerClick(object sender, EventArgs e) //Insert Button Click
{
bs.Insertintodatabase(txtname.Value, txtage.Value, txtsubject.Value);
bindgrid();
clean();
}
protected void btnupdate_ServerClick(object sender, EventArgs e) //Update Button Click
{
try
{
bs.Updateintodatabase(ddlbind.SelectedItem.Text, txtname.Value, txtage.Value, txtsubject.Value);
bindgrid();
clean();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "MessageBox", "alert('Please Select Id In Dropdown');", true);
}
}
protected void btndelete_ServerClick(object sender, EventArgs e) //Delete Button Click
{
bs.Deleteintodatabase(ddlbind.SelectedItem.Text);
bindgrid();
clean();
}
protected void ddlbind_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlbind.SelectedValue.ToString() == "0")
{
clean();
}
else
{
DataSet ds = new DataSet();
ds = bs.filltextbox(ddlbind.SelectedValue);
if (ds.Tables[0].Rows.Count > 0)
{
txtname.Value = ds.Tables[0].Rows[0]["name"].ToString();
txtage.Value = ds.Tables[0].Rows[0]["age"].ToString();
txtsubject.Value = ds.Tables[0].Rows[0]["subject"].ToString();
}
}
}
}
}
Final Output
Download Source