SIGN UP MEMBER LOGIN:    
ARTICLE

GridView Sorting, Paging without using Session, ViewState or Cache

Posted by Suthish Nair Articles | Current Affairs August 29, 2010
In this article you will learn how to use GridView Sorting, Paging without using Session, ViewState or Cache.
Reader Level:
Download Files:
 

I was wondering why most of the web sites having articles/blogs about GridView Sorting, Paging uses ViewState/Sessions objects etc for storing datatable records for further use. Another way of using was getting data from database on every request. But, if DB table data is not frequently changing, then why we need to hit DB every time. Why can't' we store the data somewhere else. Here, again came ViewState/Sessions objects etc for saving. Is there no other way of doing it? Yes, we can achieve it by using static classes.

Note:

Am trying to show how we can do it by using static classes, if my way of coding and logics is not good, please comment on it, so improvement can be done. Also, this minimizes number of coding lines.

MVS used: 2008/2010

DB used: Northwind

1. Declared a temp Dataset object ->
private static DataSet tmpds = new DataSet();,  which was used for storing records from Database and later used for sorting and paging of GridView.
2. For sorting, declared a temp String ->
static String tmpSort = " DESC";, and later using as
dataView.Sort =

e.SortExpression + (string)((e.SortDirection == SortDirection.Ascending) ? tmpSort : tmpSort);
tmpSort = (tmpSort == " ASC") ? " DESC" : " ASC";

You can see on Page Load am calling Timer event for fetching the records. Also, Timer Interval property was set to 10secs. It will trigger the event in every 10secs for fetching records from DB; this will take care of new records updations also.

Sample source codes attached for download and please post your comments.
 

Login to add your contents and source code to this article
share this article :
post comment
 

Hi, this is very nice article

Posted by Mayur Gujrathi Apr 09, 2011

Thanks for uploading such nice tutorials. I want to know how to create Chatting application in browser? using simple asp.net concept without using socket & remoting concept.Please replye as you possible.

Posted by sagar Bhosale Feb 01, 2011

One other thought.  If your web application is running on multiple servers, you may have problems with static variables.  You may also have problems using the Cache, but that's a different issue.

I've seen articles about problems with static variables in IIS.  See for example http://sharenotes.wordpress.com/2010/01/07/static-variables-issue-in-shared-hosting-or-multiple-servers/ 

So again, I think it's a clever idea, but I don't think I would try it without a lot of testing on a server farm. 

Posted by Andrew Fenster Oct 25, 2010

Hi,

I accept your comment, using cache we can do the same thing.

The article saying, if you have same data or kind of online data
thats needs to share across all users we can use statc variable.

Posted by Suthish Nair Oct 25, 2010

There's already an object to do what you want, the Cache.  You can stick your DataSet in the Cache like this:

Cache["MyDataSet"] = myDataSet;

You get it back out of the Cache like this: 

DataSet myDataSet = (DataSet)Cache["MyDataSet"];
if (myDataSet == null)
{
     myDataSet = LoadMyDataSet( );
     Cache["MyDataSet"] = myDataSet;
}

You can control how long the data remains in the Cache.  For example, you can have the Cache drop your DataSet every 20 minutes.   So the next time you need it after 20 minutes, the data will get reloaded.  There are many other features, and the Cache is optimized to work with your server. 

There's no need for you to rewrite the book when there's already something there.  If the Cache is not sufficient, there are other very well developed alternatives such as the MongoDB or SQLite. 

So I think you have a clever idea, making a static DataSet, but it's not necessary.

Posted by Andrew Fenster Oct 25, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor