3tier architecture example to insert and bind data in gridview
table
Presentation(User Interface)
Source Code for above design
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="auto-style2">
<strong>3Tier Example</strong></div>
<table class="auto-style1">
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>City</td>
<td>
<asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnsave" runat="server" OnClick="btnsave_Click" Text="save" />
</td>
</tr>
<tr>
<td> </td>
<td>
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>
DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class Class1
{
}
public class ClsDataLayer
{
SqlConnection con = new SqlConnection("data source=ravi-PC;database='3tierexample';integrated security='true';");
public void InsertData(string _name,String _city,String _email)
{
SqlDataAdapter adp = new SqlDataAdapter("insert into Usermst values('"+_name+ "','" + _city + "','" + _email + "')",con);
DataTable dt = new DataTable();
adp.Fill(dt);
}
public object SelectData()
{
SqlDataAdapter adp = new SqlDataAdapter("select *from Usermst", con);
DataTable dt = new DataTable();
adp.Fill(dt);
return dt;
}
}
}
i will give you clear explanation
follow step by step
table
Presentation(User Interface)
Source Code for above design
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="auto-style2">
<strong>3Tier Example</strong></div>
<table class="auto-style1">
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>City</td>
<td>
<asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnsave" runat="server" OnClick="btnsave_Click" Text="save" />
</td>
</tr>
<tr>
<td> </td>
<td>
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>
DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class Class1
{
}
public class ClsDataLayer
{
SqlConnection con = new SqlConnection("data source=ravi-PC;database='3tierexample';integrated security='true';");
public void InsertData(string _name,String _city,String _email)
{
SqlDataAdapter adp = new SqlDataAdapter("insert into Usermst values('"+_name+ "','" + _city + "','" + _email + "')",con);
DataTable dt = new DataTable();
adp.Fill(dt);
}
public object SelectData()
{
SqlDataAdapter adp = new SqlDataAdapter("select *from Usermst", con);
DataTable dt = new DataTable();
adp.Fill(dt);
return dt;
}
}
}
BLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using BLL;
namespace BLL
{
public class Class1
{
}
public class CLSBussLayer
{
ClsDataLayer objDAL = new ClsDataLayer();
public void InsertUser(String _name,String _city,string _email)
{
objDAL.InsertData(_name, _city, _email);
}
public object SelectUser()
{
return objDAL.SelectData();
}
}
}
Code for Button_Click
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsave_Click(object sender, EventArgs e)
{
CLSBussLayer objBLL = new CLSBussLayer();
objBLL.InsertUser(txtname.Text, txtcity.Text, txtemail.Text);
GridView1.DataSource = objBLL.SelectUser();
GridView1.DataBind();
}
}
i will give you clear explanation
follow step by step