Anda di halaman 1dari 17

9/9/2014 3-Tier Architecture in asp.

net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 1/17
HOME PHP ASP.NET JAVASCRIPT JQUERY MS SQL SERVER MYSQL WINDOWS
3-Tier Architecture in asp.net using c#
In this asp.net tutorial you will learn how to implement 3-tier architecture in asp.net using
c#. 3-Tier architecture is also called layered architecture. Some people called it n-tier
architecture. Layer architectures are essentially objects and work in object oriented
environment just like asp.net. 3-tier architecture is a very well known architecture in the
world of software development, it doesn't matter whether you are developing web based
application or desktop based, it is the best architecture to use.
3-Tier Architecture in asp.net using c#
3-Tier architecture consists of
1) UI or Presentation Layer
2) Business Access Layer or Business Logic Layer
3) Data Access Layer
Presentation Layer
Presentation layer consists of pages like .aspx or desktop based form where data is
presented to users or getting input from users.
Business Logic layer or Business Access Layer
Business logic layer contains all of the business logic. Its responsibility is to validate the
business rules of the component and communicating with the Data Access Layer. Business
Logic Layer is the class in which we write functions that get data from Presentation Layer
and send that data to database through Data Access Layer.
Data Access Layer
Data Access Layer is also the class that contains methods to enable business logic layer to
connect the data and perform desired actions. These desired actions can be selecting,
inserting, updating and deleting the data. DAL accepts the data from BAL and sends it to
the database or DAL gets the data from the database and sends it to the business layer. In
short, its responsibility is to communicate with the backend structure.
Illustration of 3-Tier Architecture with Diagram
Programming Tutorials
php tutorials, asp.net tutorials, jquery tutorials and all other web tutorials

Hello Everyone, It's me :)
Adeel Fakhar
Rawalpindi, Punjab,
Pakistan
Hi, I am Adeel Fakhar. Working in a
multinational environment for past 5
years as a Senior Software Engineer. My
Passion is Web Development. Looking
for an opportunity to work in abroad such
as USA, UK and UAE.
View my complete profile
Recent Posts
Replace empty record in ms sql
server
Check whether first character in a
Live Traffic Feed
Real-time view Menu
A visitor from Nagpur,
Maharashtra viewed "How to get
date for every sunday in a month
using asp.net with c#" 1 min ago
A visitor from Chennai, Tamil
Nadu viewed "Print using
javascript" 2 mins ago
A visitor from Mumbai,
Maharashtra left "How to get row
data of html table using jquery" via
disqus.com 6 mins ago
A visitor from Mumbai,
Maharashtra viewed "How to get
row data of html table using
jquery" 7 mins ago
A visitor from Bangladesh viewed
"How to get row data of html table
using jquery" 13 mins ago
A visitor from Seattle, Washington
viewed "How to get row data of
html table using jquery" 13 mins
ago
A visitor from Lyngby viewed
"Cross-Page Posting in asp.net
using c#" 14 mins ago
A visitor from Odense viewed
"Remove last character of a string
in javascript" 16 mins ago
A visitor from Makati, Mindoro
Occidental viewed "How to fire the
client side onclick event of link
button inside gridview in asp.net
using jquery" 18 mins ago A visitor from New Delhi, Delhi
viewed "Limit the text length in
textbox using javascript" 21 mins
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 2/17
The figure clearly describe about the purpose of BAL and DAL. The main advantage of 3-
tier architecture is to separate the presentation layer from data access layer. You will not
write any function to communicate with database in presentation layer, all the required
functions for communication with database will be available in DataAcessLayer. Its mean
at presentation layer you will just focus at information that you will present in front of
user.
I am going to create BAL, DAL in App_Code folder. You can also create separate projects
for BAL, DAL and UI (Your website) and referenced your DAL into BAL and BAL into UI. In
that scenario you have to rebuild the DAL and BAL every time, in order to view the change
that you have made in your BAL and DAL. So to get rid of rebuilding layers every time
after change, I am going to create BAL and DAL folder in App_Code. Now feel free to make
changes in BAL and DAL and just refresh the webpage to view the change that you made,
in short no rebuilding of DAL and BAL is required. The following figure shows the 3-tier
architecture of our website that we are going to made.
Design and implement 3-tier architecture
.
1) Open visual studio or visual web developer.
2) Go to File-> New Web Site
string is capital or not
Make first character of a string upper
case in asp.net
Image printing problem in google
chrome
Difference between
Response.Redirect() and
Response.RedirectPermanent()
Keep in touch - Subscribe To Feed
Subscribe in a reader
Categories
asp.net (86) javascript
(40) jquery (32) ms sql
server (39) mysql (16) php
(54)
technology news (10)
Search Web Tutorial
Search
Pageviews last month
Blog Archive
2014 (5)
2013 (13)
2012 (1)
2011 (109)
2010 (26)
November (3)
October (3)
3-Tier Architecture in asp.net using
c#
Send attachment with email in
asp.net using c#
CS0501:'blah blah' must declare a
body because it ...
September (2)
August (3)
July (2)
June (3)
May (3)
April (3)
February (1)
January (3)
2009 (93)
2008 (7)
Bookmark Us
Bookmark this Blog
Join the Community
css (1)
linq (1)
others (1)
quality assurance (1) software (1)
tools (1) visual studio
(2) windows (3) windows 7 (1) wordpress (1)
1 9 8 3 3
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 3/17
3) Select ASP.NET Web Site and then browse the Folder in which you want to save your
web pages.
4) Go to Solution Explorer and then right click on your website folder. Go to Add ASP.NET
Folder-> App_Code.
5) Now right click on App_Code Folder and select New Folder.
Join this site
with Google Friend Connect
Members (65) More
Already a member? Sign in
Recent Comments
fgb
i want checkbox coding retrive in
data in asp.net
http://nice-
tutorials.blogspot.com/2009/12/delete
-multiple-rows-in-gridview-in.html 1
week ago
mrmaf
One more thing, if you developed
blog in php and you want to integrate
it in your website which is running in
asp.net then you have to do
following...
Installing PHP on Windows 7 with IIS
7 via FastCGI 4 months ago
Si Phyo
helpful for me .thanks
Maintain scroll position on postback
in asp.net 7 months ago
mrmaf
Presentation Layer
3-Tier Architecture in asp.net using
c# 9 months ago
Lester
Is the discussion here still active? I
would like to ask something. thanks
3-Tier Architecture in asp.net using
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 4/17
6) Create Two Folders and give BusinessLayer and DataAccessLayer names to them.
7) Now right click on DataAccessLayer -> Add New Item.
8) Select Class as template and give DbAccess name to that class.
9) Now right click on BusinessLayer folder-> Add New Item
c# 10 months ago
Subscribe via Email
Get New Post in your Email Account
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 5/17
10) Select Class as template and give BusComments.cs name to that class.
Now open your DbAccess.cs file placed in DataAccessLayer folder. Clear it by deleting all
its built-in code and then copy/paste the following code in your DbAccess.cs file and then
save it
DbAccess.cs
view plain print ?
01. using System;
02. using System.Data;
03. using System.Collections;
04. using System.Collections.Generic;
05. using System.Configuration;
06. using System.Data.SqlClient;
07. using System.Web;
08.
09.
10. namespace DataAccessLayer
11. {
12. /// <summary>
13. /// Constains overloaded method to access database and run queries
14. /// </summary>
15. public class DbAccess
16. {
17. private SqlConnection DbConn = new SqlConnection();
18. private SqlDataAdapter DbAdapter = new SqlDataAdapter();
19. private SqlCommand DbCommand = new SqlCommand();
20. private SqlTransaction DbTran;
21. private string strConnString = ConfigurationManager.ConnectionStrings["WebsiteConnectionString"].ToString();
22.
23.
24. public void setConnString(string strConn)
25. {
26. try
27. {
28. strConnString = strConn;
29. }
30. catch (Exception exp)
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 6/17
31. {
32. throw exp;
33. }
34. }
35.
36.
37. public string getConnString()
38. {
39. try
40. {
41. return strConnString;
42. }
43. catch (Exception exp)
44. {
45. throw exp;
46. }
47. }
48.
49.
50. private void createConn()
51. {
52. try
53. {
54.
55.
56. DbConn.ConnectionString = strConnString;
57. DbConn.Open();
58.
59.
60. }
61. catch (Exception exp)
62. {
63. throw exp;
64. }
65. }
66. public void closeConnection()
67. {
68. try
69. {
70. if (DbConn.State != 0)
71. DbConn.Close();
72. }
73. catch (Exception exp)
74. {
75. throw exp;
76. }
77. }
78.
79.
80. public void beginTrans()
81. {
82. try
83. {
84. if (DbTran == null)
85. {
86. if (DbConn.State == 0)
87. {
88. createConn();
89. }
90.
91.
92. DbTran = DbConn.BeginTransaction();
93. DbCommand.Transaction = DbTran;
94. DbAdapter.SelectCommand.Transaction = DbTran;
95. DbAdapter.InsertCommand.Transaction = DbTran;
96. DbAdapter.UpdateCommand.Transaction = DbTran;
97. DbAdapter.DeleteCommand.Transaction = DbTran;
98.
99.
100. }
101.
102.
103. }
104. catch (Exception exp)
105. {
106. throw exp;
107. }
108. }
109. public void commitTrans()
110. {
111. try
112. {
113. if (DbTran != null)
114. {
115. DbTran.Commit();
116. DbTran = null;
117. }
118.
119.
120. }
121. catch (Exception exp)
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 7/17
122. {
123. throw exp;
124. }
125. }
126. public void rollbackTrans()
127. {
128. try
129. {
130. if (DbTran != null)
131. {
132. DbTran.Rollback();
133. DbTran = null;
134. }
135.
136.
137. }
138. catch (Exception exp)
139. {
140. throw exp;
141. }
142. }
143.
144.
145. /// <summary>
146. /// Fills the Dataset dset and its Table tblname via stored procedure provided as spName arguement.Takes Parameters as param
147. /// </summary>
148. /// <param name="dSet"></param>
149. /// <param name="spName"></param>
150. /// <param name="param"></param>
151. /// <param name="tblName"></param>
152. public void selectStoredProcedure(DataSet dSet, string spName, Hashtable param, string tblName)
153. {
154. try
155. {
156. if (DbConn.State == 0)
157. {
158. createConn();
159. }
160. DbCommand.Connection = DbConn;
161. DbCommand.CommandText = spName;
162. DbCommand.CommandType = CommandType.StoredProcedure;
163. foreach (string para in param.Keys)
164. {
165. DbCommand.Parameters.AddWithValue(para, param[para]);
166.
167.
168. }
169.
170.
171. DbAdapter.SelectCommand = (DbCommand);
172. DbAdapter.Fill(dSet, tblName);
173. }
174. catch (Exception exp)
175. {
176.
177.
178. throw exp;
179. }
180. }
181.
182.
183. public void selectStoredProcedure(DataSet dSet, string spName, string tblName)
184. {
185. try
186. {
187. if (DbConn.State == 0)
188. {
189. createConn();
190. }
191. DbCommand.Connection = DbConn;
192. DbCommand.CommandText = spName;
193. DbCommand.CommandType = CommandType.StoredProcedure;
194. DbAdapter.SelectCommand = DbCommand;
195. DbAdapter.Fill(dSet, tblName);
196. }
197. catch (Exception exp)
198. {
199. throw exp;
200. }
201. }
202.
203.
204. public void selectQuery(DataSet dSet, string query, string tblName)
205. {
206. try
207. {
208. if (DbConn.State == 0)
209. {
210. createConn();
211. }
212. DbCommand.CommandTimeout = 600;
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 8/17
213. DbCommand.Connection = DbConn;
214. DbCommand.CommandText = query;
215. DbCommand.CommandType = CommandType.Text;
216. DbAdapter = new SqlDataAdapter(DbCommand);
217. DbAdapter.Fill(dSet, tblName);
218. }
219. catch (Exception exp)
220. {
221. DbAdapter.Dispose();
222. DbConn.Close();
223. throw exp;
224. }
225. finally
226. {
227. DbAdapter.Dispose();
228. DbConn.Close();
229. }
230. }
231.
232.
233. public int executeQuery(string query)
234. {
235. try
236. {
237.
238.
239. if (DbConn.State == 0)
240. {
241. createConn();
242. }
243. DbCommand.Connection = DbConn;
244. DbCommand.CommandText = query;
245. DbCommand.CommandType = CommandType.Text;
246. return DbCommand.ExecuteNonQuery();
247. }
248. catch (Exception exp)
249. {
250. throw exp;
251. }
252. finally
253. {
254. DbAdapter.Dispose();
255. DbConn.Close();
256. }
257. }
258. public int executeStoredProcedure(string spName, Hashtable param)
259. {
260. try
261. {
262. if (DbConn.State == 0)
263. {
264. createConn();
265. }
266. DbCommand.Connection = DbConn;
267. DbCommand.CommandText = spName;
268. DbCommand.CommandType = CommandType.StoredProcedure;
269. foreach (string para in param.Keys)
270. {
271. DbCommand.Parameters.AddWithValue(para, param[para]);
272. }
273. return DbCommand.ExecuteNonQuery();
274. }
275. catch (Exception exp)
276. {
277. throw exp;
278. }
279. }
280. public int returnint32(string strSql)
281. {
282. try
283. {
284. if (DbConn.State == 0)
285. {
286.
287.
288. createConn();
289. }
290. else
291. {
292. DbConn.Close();
293. createConn();
294. }
295. DbCommand.Connection = DbConn;
296. DbCommand.CommandText = strSql;
297. DbCommand.CommandType = CommandType.Text;
298. return (int)DbCommand.ExecuteScalar();
299. }
300. catch (Exception exp)
301. {
302. return 0;
303. }
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 9/17
304. }
305. public Int64 returnint64(string strSql)
306. {
307. try
308. {
309. if (DbConn.State == 0)
310. {
311. createConn();
312. }
313. DbCommand.Connection = DbConn;
314. DbCommand.CommandText = strSql;
315. DbCommand.CommandType = CommandType.Text;
316. return (Int64)DbCommand.ExecuteScalar();
317. }
318. catch (Exception exp)
319. {
320. throw exp;
321. }
322. }
323. public int executeDataSet(DataSet dSet, string tblName, string strSql)
324. {
325. try
326. {
327. if (DbConn.State == 0)
328. {
329. createConn();
330. }
331.
332.
333. DbAdapter.SelectCommand.CommandText = strSql;
334. DbAdapter.SelectCommand.CommandType = CommandType.Text;
335. SqlCommandBuilder DbCommandBuilder = new SqlCommandBuilder(DbAdapter);
336.
337.
338. return DbAdapter.Update(dSet, tblName);
339. }
340. catch (Exception exp)
341. {
342. throw exp;
343. }
344. }
345.
346.
347. public bool checkDbConnection()
348. {
349. int _flag = 0;
350. try
351. {
352. if (DbConn.State == ConnectionState.Open)
353. {
354. DbConn.Close();
355. }
356.
357.
358. DbConn.ConnectionString = getConnString();
359. DbConn.Open();
360. _flag = 1;
361. }
362. catch (Exception ex)
363. {
364. _flag = 0;
365. }
366. if (_flag == 1)
367. {
368. DbConn.Close();
369. _flag = 0;
370. return true;
371. }
372. else
373. {
374. return false;
375. }
376.
377.
378. }
379. public string GetColumnValue(string Query)
380. {
381. try
382. {
383. if (DbConn.State == 0)
384. {
385. createConn();
386. }
387. DbCommand.CommandTimeout = 120;
388. DbCommand.Connection = DbConn;
389. DbCommand.CommandType = CommandType.Text;
390. DbCommand.CommandText = Query;
391.
392.
393. object objResult = DbCommand.ExecuteScalar();
394. if (objResult == null)
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 10/17
395. {
396. return "";
397. }
398. if (objResult == System.DBNull.Value)
399. {
400. return "";
401. }
402. else
403. {
404. return Convert.ToString(objResult);
405. }
406. }
407. catch (Exception ex)
408. {
409. throw ex;
410. }
411. finally
412. {
413. DbAdapter.Dispose();
414. DbConn.Close();
415. }
416. }
417. }
418. }
I will not go into any detail of the code written in DbAccess.cs file but I will tell you three
main things about my DbAccess.cs file
1) I have created a namespace DataAccessLayer and place the DbAccess class inside
the namespace. Namespaces are a way to define the classes and other types of data into
one hierarchical structure. System is the basic namespace used by every .NET page. A
namespace can be created via the Namespace keyword just like I did.
2) private string strConnString =
ConfigurationManager.ConnectionStrings["WebsiteConnectionString"].ToString() contains
the name of the connection string(WebsiteConnectionString) that I declared in web.config
file.
3) DbAccess class contains all the methods to communicate with database via query and
store procedures. It contains all the methods for you to perform the select, insert, update
and delete the data.
Now open your BusComments.cs file and clear it by deleting all the built-in code and then
copy/paste the below mention code in it.
BusComments.cs
view plain print ?
01. using System;
02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05. using System.Data;
06. using DataAccessLayer;
07.
08.
09.
10.
11. namespace BusinessLayer
12. {
13. public class BusComments
14. {
15. DbAccess _dbAccess = new DbAccess();
16. private DataSet _CommentsDS = new DataSet();
17. public DataSet CommentsDS
18. {
19. get
20. {
21. return _CommentsDS;
22. }
23. set
24. {
25. _CommentsDS = value;
26. }
27. }
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 11/17
28.
29.
30. public void getComments()
31. {
32. try
33. {
34. string strSQL = "SELECT * from comments";
35. //Creating Datatable, if datatable not exist already.
36. //The data return by query will be stored in DataTable.
37. if (_CommentsDS.Tables.Contains("GetComments"))
38. {
39. _CommentsDS.Tables.Remove("GetComments");
40. }
41.
42.
43. _dbAccess.selectQuery(_CommentsDS, strSQL, "GetComments");
44.
45.
46. }
47. catch (Exception ex)
48. {
49. throw ex;
50. }
51. }
52. }
53. }
Same as DbAccess.cs, I have created a namespace BusinessLayer and put
BusComments class in it. I have declared a private dataset _CommentsDS and define a
public dataset _CommentsDS in BusComments class. After interacting with database,
Private Dataset will return all the data to the public dataset so that data can be accessible
to the Presentation Layer. You may notice that I have used the DataAccessLayer
namespace with other namespaces. I have also created the object of DbAccess class to get
the appropriate methods written inside it to communicate with database; its all possible
due to the inclusion of DataAccessLayer namespace. So dont forget to include
DataAccessLayer namespace in your every business layer class.
Now open the Default.aspx file and again clear all the pre written code in it and then
copy/paste the following code in it.
Default.aspx
view plain print ?
01. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
02.
03.
04. <!DOCTYPE html PUBLIC "-
//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
05. <html xmlns="http://www.w3.org/1999/xhtml">
06. <head runat="server">
07. <title>Designing and implementing the 3-tier architecture in asp.net</title>
08. <style type="text/css">
09. .textDIV
10. {
11. font-family: Verdana;
12. font-size: 12px;
13. }
14. </style>
15. </head>
16. <body>
17. <form id="form1" runat="server">
18. <div class="textDIV">
19. <table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">
20. <tr>
21. <td style="width: 45%">
22.
23.
24. </td>
25. <td>
26.
27.
28. </td>
29. <td>
30.
31.
32. </td>
33. </tr>
34. <tr>
35. <td width="20%" colspan="3" align="center">
36. <span style="display: inline;"><strong>Comments</strong></span>
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 12/17
37. </td>
38. </tr>
39. <tr>
40. <td width="20%" colspan="3">
41.
42.
43. </td>
44. </tr>
45. <tr>
46. <td colspan="3" align="center">
47. <hr style="width: 50%;" />
48. </td>
49. </tr>
50. <tr>
51. <td>
52.
53.
54. </td>
55. </tr>
56. <tr>
57. <td align="center" width="100%">
58. <asp:GridView ID="GridView1" runat="server" EnableTheming="false" AutoGenerateColumns="false"
59. GridLines="None" OnRowDataBound="GridView1_RowDataBound" Width="660px" HorizontalAlign="Center">
60. <Columns>
61. <asp:TemplateField HeaderText="Sr No" HeaderStyle-Width="15%" HeaderStyle-
HorizontalAlign="Center">
62. <ItemTemplate>
63. <%# Container.DataItemIndex + 1 %>
64. </ItemTemplate>
65. <ItemStyle HorizontalAlign="Center" />
66. </asp:TemplateField>
67. <asp:TemplateField HeaderText="First Name" HeaderStyle-Width="15%" HeaderStyle-
HorizontalAlign="Left">
68. <ItemTemplate>
69. <asp:Label ID="lblFirstName" runat="server" EnableTheming="false" Text='<%# Bind("first_name")%>'>
</asp:Label>
70. </ItemTemplate>
71. <ItemStyle HorizontalAlign="Left" />
72. </asp:TemplateField>
73. <asp:TemplateField HeaderText="Last Name" HeaderStyle-Width="15%" HeaderStyle-
HorizontalAlign="Left">
74. <ItemTemplate>
75. <asp:Label ID="lblLastName" runat="server" EnableTheming="false" Text='<%# Bind("last_name")%>'>
</asp:Label>
76. </ItemTemplate>
77. <ItemStyle HorizontalAlign="Left" />
78. </asp:TemplateField>
79. <asp:TemplateField HeaderText="Comments" HeaderStyle-Width="50%" HeaderStyle-
HorizontalAlign="Left">
80. <ItemTemplate>
81. <asp:Label ID="lblComments" runat="server" EnableTheming="false" Text='<%# Bind("comments")%>'>
</asp:Label>
82. </ItemTemplate>
83. <ItemStyle HorizontalAlign="Left" />
84. </asp:TemplateField>
85. </Columns>
86. </asp:GridView>
87. </td>
88. </tr>
89. <tr>
90. <td>
91.
92.
93. </td>
94. </tr>
95. <tr>
96. <td align="center">
97. <input type="button" value="Back" onclick="history.back(-1);" />
98. </td>
99. </tr>
100. </table>
101. </div>
102. </form>
103. </body>
104. </html>
As you have seen that in .aspx file I just placed the asp:GridView control. Now let's have a
look over the Default.aspx.cs file
Default.aspx.cs
view plain print ?
01. using System;
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 13/17
02. using System.Collections;
03. using System.Configuration;
04. using System.Data;
05. using System.Linq;
06. using System.Web;
07. using System.Web.Security;
08. using System.Web.UI;
09. using System.Web.UI.HtmlControls;
10. using System.Web.UI.WebControls;
11. using System.Web.UI.WebControls.WebParts;
12. using System.Xml.Linq;
13. using BusinessLayer;
14.
15.
16. public partial class Default : System.Web.UI.Page
17. {
18. BusComments _objComments = new BusComments();
19. protected void Page_Load(object sender, EventArgs e)
20. {
21. if (!Page.IsPostBack)
22. {
23. BindGrid();
24. }
25. }
26.
27.
28. public void BindGrid()
29. {
30. _objComments.getComments();
31. //Tables[GetComments] is the DataTable that we have created in BusComments class
32. GridView1.DataSource = _objComments.CommentsDS.Tables["GetComments"];
33. GridView1.DataBind();
34. }
35.
36.
37. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
38. {
39. if (e.Row.RowType == DataControlRowType.DataRow)
40. {
41. ((Label)e.Row.FindControl("lblComments")).Text = ((Label)e.Row.FindControl("lblComments")).Text.ToString().ToUpper();
42. }
43.
44.
45. }
46. }
I have included the BusinessLayer namespace on the top so that I can get access to the
BusComments class. Then I initialized the object of BusComments class and then by using
the object I have called my required function placed in BusComments class to get data and
populate the GridView. Now you have seen I called the function written in BusComments
class to get the data from database and that function actually called its required function
written in DbAccess class to get the data from database. DbAccess gets the data from
database and return to BusComments Class, BusComments class gets the data
from DbAccess class and return to Presentation Layer which is default.aspx
Update Note at May-01-2011
I have seen through tracking software that this post is very much popular among my
respected users. So i have decided to write further posts on this topic, such as how to
insert, delete, update records in database using 3-tier architecture. So keep in touch with
nice-tutorials:)
Update Note at May-05-2011
Just now i have written post on Insertion of records in database using 3-tier architecture in
asp.net with c#. More to come soon on this topic. Keep in touch.
Update Note at June-01-2011
Just now i have written post on Delete records in database using 3-tier architecture in
asp.net with c#. More to come soon on this topic. Stay tune :)
Update Note at August-09-2011
Sorry for the delay. Just now i have written post on Update records in database using 3-
tier architecture in asp.net with c#. Stay tuned. This topic is not over yet. Soon i will write
posts for jquery and 3-tier architecture in asp.net with c#. Jquery will boost the
performance of your website, boost up the speed of your website and also make your
website light. :)
So this is the way to design and implement the 3-tier architecture/layered architecture/n-
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 14/17
49
Related Posts
Make first character of a string upper case in asp.net
Difference between Response.Redirect() and Response.RedirectPermanent()
Create Fake Query in LINQ to avoid unknown return type error while drag and drop sp
in dbml
Unable to work on visual studio after windows update installed, visual studio close
automatically
Check whether first character in a string is capital or not
tier architecture in asp.net using c#.
Labels: asp.net
25 Comments
25 Comments Nice Tutorials Blog Login

1
Sort by Oldest Share

Join the discussion


Reply
Pete 3 years ago
You are right, this IS good ! many thanks sir !


Reply
Gene Magerr 3 years ago
This is very clean. I'm following the tutorials for creating 3-tier website here
http://www.asp.net/data-access... I am using TableAdapters with Stored
Procedures. I've created all of the Business Logic Layer Classes for each of my
tables in the DB, and i would prefer to create Data Access Layers for each table
as well. The DAL in your tutorial seems to be the only one needed, in other
words it appears it's a single point of entry to the database, and you don't need to
create a seperate DAL for each table. Can you please explain your DBAccess.cs
in detail so I understand exactly how you're interacting with the Database? I
would really appreciate a detailed breakdown.
Thanks for the great tutorial


Reply
mrmaf 3 years ago Mod > Gene Magerr
Well Gene Magerr, definitely i will tell you but right now i am busy with
other posts. Stay tune, i will discuss each and every function written in
dbacces.cs


Reply
Genemagerr 3 years ago > mrmaf
mrmaf,
I've bookmarked several nTier webpages, and IMHO this is by far
the best. I look forward to your post as I really want to understand
the code
Thanks


Favorite

Share
Share
Share
Share
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 15/17
Reply


Reply
K_hemmbram 3 years ago
Sir, I want to edit the records from gridview in 3 tier architech. please help me.


Reply
mrmaf 3 years ago Mod > K_hemmbram
Within two days i will write a post about editing records in 3-tier
architecture. So stay tune.


Reply
mrmaf 3 years ago Mod > mrmaf
Hi K_hemmbram sorry for delay, anyways edit the records from
gridview in 3-tier architecture tutorial is available now. Once again
sorry for delay as i was busy in other things.

1


Reply
Jaheenajamal 3 years ago
this very good article to people like me.


Reply
Sudarshan 3 years ago
sir,
Please can you upload this application with database file.That'd help us a
lot(even more)
.


Reply
mrmaf 3 years ago Mod > Sudarshan
Hi sudarshan, nice to see you here. Anyways sudarshan, i have flagged
your message and soon i will put all the code of 3-tier architecture in a zip
file, that zip file will also contain insertion, deletion and updating of
records. Take care


Reply
Nag 3 years ago
this is good code

1


Reply
Nag 3 years ago
niceeeeeeeeeeeeee


Reply
Hari 3 years ago
Great Post...Explained with simple words and also provide enough screen shots.
Thank you so much..

1


Reply
N-Tier Application Services 3 years ago
Great explanation and a Great post indeed! Great to read it!


Reply
Red Srinu 3 years ago > N-Tier Application Services
great explanation thank you I have got clear understood from this
arithmetical only

1


Reply
Quest_capital 3 years ago
Where can I download this code?


Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
Share
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 16/17
Newer Post Older Post
Reply
Trivikram 2 years ago
thanks sir !
it's very useful for me


Reply
Balaji 2 years ago
Super...


Reply
Niraj Zambad 2 years ago > Balaji
What is the need of getting and setting the Connection String in
DbAccess.cs? As it will be already defined in web.config


Reply
Haikaz2707 2 years ago
hye, i try to run the dataaccess layer, i don't know i stuck at the BeginTrans()
function.
i try to debug line by line, after
DbAdapter.SelectCommand.Transaction = DbTrans;
the program return error message at catch
"Object reference not set to an instance of an object"
Please help.
Thanks


Reply
prabhanjan 2 years ago
thank u very much..! now i understood what 3tire architecture is.


Reply
mangesh bankar 2 years ago
Where can I download this code? insert update & delete record in 3 Tier Apps.
Thanks


Reply
ran a year ago
Which layer does JQuery belong to ?


Reply
mrmaf 9 months ago Mod > ran
Presentation Layer


Reply
Lester 10 months ago
Is the discussion here still active? I would like to ask something. thanks


Subscribe

Add Disqus to your site


d
Share
Share
Share
Share
Share
Share
Share
Share
Share
Home
Subscribe to: Post Comments (Atom)
9/9/2014 3-Tier Architecture in asp.net using c#
http://nice-tutorials.blogspot.in/2010/10/3-tier-architecture-in-aspnet-using-c.html 17/17