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

using cache to store Data in datatable for custom paging with Datalist, Repeater or Datagrid

Rate me:
Please Sign up or sign in to vote.
3.42/5 (7 votes)
1 Jun 20064 min read 99.5K   283   36  
In this article, I ll show you how to cache data to use with DataList/Repeater/Datagrid controls for custom paging and better performance
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.Data.SqlClient;

namespace SearchC
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.HyperLink hpPre;
		protected System.Web.UI.WebControls.HyperLink hpNext;
		protected System.Web.UI.WebControls.TextBox txtSearch;
		protected System.Web.UI.WebControls.DataList DataList1;
		protected System.Web.UI.WebControls.TextBox txtPageSize;
		protected System.Web.UI.WebControls.TextBox txtPageNum;
		protected System.Web.UI.WebControls.Label lblTotal;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (Request.QueryString["p"] != null)
			{
				txtPageNum.Text = Request.QueryString["p"].ToString();
			}
			if (Request.QueryString["s"] != null)
			{
				txtSearch.Text = Request.QueryString["s"].ToString();
			}
			if (!Page.IsPostBack)
			{
				sortPage();
			}
			
		}

		private void sortPage()
		{
			string iID = Session.SessionID.ToString();
			object dd = Cache.Get(iID);
			DataTable dt = new DataTable();
			string SearchText = txtSearch.Text;
			DataTable table = new DataTable();
			DataTable Table = new DataTable();
			int tot = 0;
			if (dd == null)
			{
				SqlConnection Con = new SqlConnection("server=(local);database=NorthWind;uid=sa;pwd=sa;");
				SqlCommand Com = new SqlCommand();
				Com.Connection = Con;
				Com.CommandText = "SELECT ProductName FROM Products WHERE ProductName LIKE '%" + SearchText + "%'";
				SqlDataAdapter da = new SqlDataAdapter(Com);
				da.Fill(dt);
				if (Con.State == ConnectionState.Open)
				{
					Con.Close();
				}
				Com.Dispose();
			}
			if (dd == null)
			{
				DataColumn column;
				DataRow row;
				column = new DataColumn();
				column.DataType = System.Type.GetType("System.Int32");
				column.AutoIncrement = true;
				column.ColumnName = "iID";
				column.ReadOnly = true;
				column.Unique = true;
				column.AutoIncrementSeed = 1;
				column.AutoIncrementStep = 1;
				// Add the Column to the DataColumnCollection.
				table.Columns.Add(column);

				column = new DataColumn();
				column.DataType = System.Type.GetType("System.String");
				column.ColumnName = "ProductName";
				column.AutoIncrement = false;
				column.Caption = "ProductName";
				column.ReadOnly = false;
				column.Unique = false;
				// Add the column to the table.
				table.Columns.Add(column);
				tot = dt.Rows.Count;
				for (int i = 0; i < tot; i++)
				{
					row = table.NewRow();
					row["iID"] = i;
					row["ProductName"] = dt.Rows[i].ItemArray[0];
					table.Rows.Add(row);
				}
				Cache.Insert(iID,table,null,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(20));
			}
	
			Table = (DataTable)Cache.Get(iID);
			tot = Table.Rows.Count;
			int PageSize = Convert.ToInt32(txtPageSize.Text);
			int PageNum = Convert.ToInt32(txtPageNum.Text);
			lblTotal.Text = tot.ToString();
			int TotPages = tot / PageSize;
			if (Request.QueryString["p"] != null)
			{
				int rP = Convert.ToInt32(Request.QueryString["p"]);
				if (rP == TotPages)
				{
					hpNext.Visible = false;
					hpPre.Visible = true;
					int p = rP - 1;
					hpPre.NavigateUrl = "WebForm1.aspx?s=" + txtSearch.Text + "&p=" + p.ToString();
				}
				else
				{
					int n = rP + 1;
					int p = rP - 1;
					hpNext.NavigateUrl = "WebForm1.aspx?s=" + txtSearch.Text + "&p=" + n.ToString();
					hpPre.NavigateUrl = "WebForm1.aspx?s=" + txtSearch.Text + "&p=" + p.ToString();
				}
				if (rP == 1)
				{
					hpPre.Visible = false;
				}
			}
			else
			{
				hpNext.NavigateUrl = "WebForm1.aspx?s=" + txtSearch.Text + "&p=2";
				hpPre.Visible = false;
			}

			int FromID = ((PageNum - 1) * PageSize) + 0;
			int ToID = PageNum * PageSize;
			if (tot < ToID)
			{
				ToID = tot;
			}

			DataView dv;
			dv = Table.DefaultView;
			dv.RowFilter = "iID >= " + FromID + " AND iID <= " + ToID + "";
//			System.Data.DataTable table1 = new DataTable();
//			DataColumn column1;
//			DataRow row1;
//			column1 = new DataColumn();
//			column1.DataType = System.Type.GetType("System.Int32");
//			column1.AutoIncrement = true;
//			column1.ColumnName = "iID";
//			column1.ReadOnly = true;
//			column1.Unique = true;
//			column1.AutoIncrementSeed = 1;
//			column1.AutoIncrementStep = 1;
//			// Add the Column to the DataColumnCollection.
//			table1.Columns.Add(column1);
//
//			column1 = new DataColumn();
//			column1.DataType = System.Type.GetType("System.String");
//			column1.ColumnName = "ProductName";
//			column1.AutoIncrement = false;
//			column1.Caption = "ProductName";
//			column1.ReadOnly = false;
//			column1.Unique = false;
//			// Add the column to the table.
//			table1.Columns.Add(column1);
//
//			if (FromID == 1)
//			{
//
//			}
//			for (int i = FromID; i < ToID; i++)
//			{
//				row1 = table1.NewRow();
//				row1["iID"] = i;
//				row1["ProductName"] = table.Rows[i].ItemArray[1];
//				table1.Rows.Add(row1);
//			}
//	
			DataList1.DataSource = dv;
			DataList1.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.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Hi,
i am working as web developer in Preston,UK and working with ASp .NET (C# and VB .NET), SQL SERVER for the last 2 years i think. i had some experience in ASP before working with .NET. In my spare time I like to watch and play cricket and PC games. you can contact me at

http://www.eboxzone.com


Comments and Discussions