Click here to Skip to main content
15,897,371 members
Articles / Web Development / ASP.NET

A generic loading of data in a DropDownList using caching mechanism

Rate me:
Please Sign up or sign in to vote.
4.42/5 (7 votes)
6 Apr 2005CPOL4 min read 74.1K   485   35  
This article shows you how to make use of DataAccessLayer and caching.
using System;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
	/// <summary>
	/// Summary description for DataAccessLayer.
	/// This is a generic code for loading data from any database..
	/// One can also use OleDb for more generalized coding....
	
	/// </summary>
	public class DataAccess
	{
		//Private member
		private string strConn;
		private string strSelectCat;

		public DataAccess()
		{
			//
			// TODO: Add constructor logic here
			//Initialization 
			strConn=null;
			strSelectCat=null;
		}
		public DataAccess(string _strConn,string _strSelectCat)
		{
			//parameterized constructor;
			strConn=_strConn;
			strSelectCat=_strSelectCat;
		
		}
		//set connectionstring property 
		//this has been done just to show how property can be defined 
		
		public string ConnectionString
		{
			get
			{
				return strConn;
			}
			set
			{
				strConn=value;
			}
		
		}
		public string SelectStmOnCat
		{
			get
			{
				return strSelectCat;
			}
			set
			{
				strSelectCat=value;
			}
		}
		//PreCondition:Take Connection Object, Sql Query
		//PostCondition:Return DataTable containing Category list
		//Process:Fill Datatable with category from Northwind _db

		public DataTable LoadCategoryInDropDownList()
		{
			SqlConnection objConn=new SqlConnection(strConn);
			DataTable tblCategory =new DataTable();

			try
			{
				objConn.Open();
				//Using helps to dispose object as soon as 
				//required process gets complete
				using(SqlDataAdapter objAdpOnCat=new SqlDataAdapter(strSelectCat,objConn))
				{

					objAdpOnCat.Fill(tblCategory);
					
				}

			
			}
			catch(SqlException e)
			{
				throw new Exception("Invalid Connection Error Occured  "+e.Message);

			}
			finally
			{
				if(objConn.State.ToString()=="Open")
				{
					objConn.Dispose();
					objConn.Close();
					
				}
			}
			return tblCategory;
		
		
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead
Australia Australia
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

Comments and Discussions