Click here to Skip to main content
15,896,063 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.
/* Author :          Santosh Poojari
 * Date of Creation: 6th April 2005
 * Release date :    6th april 2005
 * Objective:		 Caching,Connection String in Web.confiq file
 *					 Implementation of DataAccessLayer
 *					 Generic Reusable Oops Concept
 *					 Loading DropDownList with Database aswell as Static data in Presetation Layer
 */


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using DataAccessLayer;
namespace CboDropDown
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DropDownList cboCategories;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.Label Label4;
		protected System.Web.UI.WebControls.Label Label5;
		protected System.Web.UI.WebControls.Label Label6;
	
		protected System.Web.UI.WebControls.Label lblselectedItemm;

		protected static string strConn=System.Configuration.ConfigurationSettings.AppSettings["DSNConn"];
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!IsPostBack)
			{
				FetchCategory();
			}
			else
			{
			//add any process if required on PostBack Condition
			}
		}
		//This will load DropDown with Data
		public void FetchCategory()
		{
			string strSelectCat="SELECT CategoryID, CategoryName FROM Categories";
			
			//Create object for DataAccess class 
			
			DataAccessLayer.DataAccess objDac=new DataAccess(strConn,strSelectCat);
			

			//The property Accessors

			/*DataAccessLayer.DataAccess objDac=new DataAccess();
			objDac.ConnectionString=strConn;
			objDac.SelectStmOnCat=strSelectCat;
			*/


			/*
			Caching helps when page is loaded again 
			after swithcing from another aspx page
			because of caching ,data is not loaded from Database but from cache memory
			This help to increase performance...........
			*/
			DataTable tblCategories = (DataTable) Cache["Categories"];

			if (tblCategories == null) 
			{
				tblCategories = new DataTable();
				tblCategories=objDac.LoadCategoryInDropDownList();
				// It inserts new row in filled datatable.
				//This new row contains static data for user instruction Text such as" Select the Item"
				DataRow dr=tblCategories.NewRow();
				dr["CategoryID"]=0;
				dr["CategoryName"]="--Select Item--";
				tblCategories.Rows.InsertAt(dr,0);
				//Cache The DataTable in a Cache Memory for duration of one hour...
				Cache.Insert("Categories", tblCategories, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
			}
			
			cboCategories.DataSource = tblCategories;
			cboCategories.DataMember = "";
		
			cboCategories.DataTextField = "CategoryName";
			cboCategories.DataValueField = "CategoryID";
				
			cboCategories.DataBind();
			
			}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.cboCategories.SelectedIndexChanged += new System.EventHandler(this.cboCategories_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void cboCategories_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			
			string iSelectedItem;
			iSelectedItem="The item you have selected is:";
			iSelectedItem+=cboCategories.SelectedItem;
			lblselectedItemm.Text=(iSelectedItem);

		}
	}
}

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