Click here to Skip to main content
13,453,957 members (44,474 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 21 Jul 2010

Writing DB Classes for the web

, 23 Jul 2010
Rate this:
Please Sign up or sign in to vote.
write classes meant for use in aspx code behind pages which talk with the database
Since this is a tip, I am making this as short as possible. In essence, this tip utilizes facts from the ASP.NET page life cycle model, garbage collection, and the concept of delegation.

Let's assume you are writing a class that does some database access to find, for e.g.
(a) if an employee actually exists in your organization, and/or
(b) if an employee has access to some department archives, etc.

You'd probably write a helper class where (a) and (b) become two methods of that class.

Suppose you are trying to do both the activities with the same connection object (which is private to your helper class) you'd need two extra method: one which opens a connection to the database, and the other which closes the connection. This approach relies on creating two additional methods.

Alternatively, people tend to traditionally open the connection, do the data access, close the connection; this is done for both operations (a) & (b). Here the system spends time opening the connection, and closing it, each time some method which does data access is called.

But fortunately using those three simple concepts in the beginning, you can now write classes which need only a single connection to do everything. The connection closes automatically, and opens automatically (at the time of constructing).

Here is a simple code example of how to achieve this:

using System.Web.UI

public class EmployeeHelper
	public EmployeeHelper(string EmployeeID)
		Page CurrentPage = HttpContext.Current.Handler as Page;
		if(CurrentPage != null)
			//opening connection
			cxn = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ConnectionString);
			cmd = cxn.CreateCommand();

			//wiring to the page's unload event here
			CurrentPage.Unload += new System.EventHandler(CurrentPage_Unload);			
			//you need some other way to open and close 
			//	database connections!!!

	SqlConnection cxn;
	SqlCommand cmd;

	void CurrentPage_Unload(object sender, EventArgs e)
		//close connection here

	//class methods
	public bool AssertValidEmployee()
		//call backend proc here
		//	no need of opening connection

	public bool AssertDepartmentAccess(int DeptID)
		//call the required backend proc here
		//	no need of opening connection



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) iGATE Global Solutions
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Thanks for sharing this tip Pin
linuxjr23-Jul-10 12:02
memberlinuxjr23-Jul-10 12:02 
GeneralReason for my vote of 5 Good, very helpful to create high pe... Pin
Ravi LVS22-Jul-10 16:26
memberRavi LVS22-Jul-10 16:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180321.1 | Last Updated 23 Jul 2010
Article Copyright 2010 by deostroll
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid