0

I use RadGrid control on my web site and i enable paging for radgrid and i monitor its activity on sql profiler and i know that radgrid fetch data for each paging and then show the data for current page.

 DataSet obj_Dataset = new DataSet(); //  is global

This is my code i write this in Page_Load :

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection obj_SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConection"].ToString());
        SqlCommand obj_sqlCommand = new SqlCommand();
        obj_sqlCommand.CommandText = "select ProductID,ProductName,CategoryID from products";
        obj_sqlCommand.CommandType = CommandType.Text;
        obj_sqlCommand.Connection = obj_SqlConnection;

        SqlDataAdapter obj_DataAdapter = new SqlDataAdapter(obj_sqlCommand);
        obj_SqlConnection.Open();
        obj_DataAdapter.Fill(obj_Dataset, "Products");
        obj_DataAdapter.Dispose();
        obj_sqlCommand.Dispose();
        obj_SqlConnection.Close();
    }

and this is my code on RadGrid1_NeedDataSource :

 protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {

        RadGrid1.DataSource = obj_Dataset.Tables["Products"];

    }

I am using ASP.net 2010 and Sqlserver 2008.

4
  • is my code wrong? why it read data from data base each time that i chang my page. Apr 23, 2012 at 18:31
  • can i do something that radgrid read data once and use it evry time. Apr 23, 2012 at 18:32
  • I use "Session["Cone"] = (DataSet)obj_Dataset;" on page load.and read "obj_Dataset = (DataSet)Session["Cone"];" on RadGrid1_NeedDataSource and i solve the problem.but i think this is not a good way couse i am using the servers memory and this may be very bad. is there a better way ? Apr 23, 2012 at 18:57
  • Using session is definitely not the recommended way to go about what you are doing. See my answer. :) Apr 23, 2012 at 18:59

2 Answers 2

2

When you implement paging on a RadGrid, each time you change the page it will retrieve only the records it needs for the current page. That's actually one of the benefits of paging. If you wanted it to only get data the first time the page is loaded, you would have to persist the entire data set through every postback thereafter.

I understand your desire to minimize database connections, but if you have enough records to require paging, you are probably better off allowing paging to work as designed, rather than trying to hold your whole dataset on the server or in the ViewState. It may also be worth considering why you are trying to avoid the database. Is it too slow? You may want to optimize your database indexes, evaluate your structure, or look into caching.

However, if this is something your really want to do, and you understand the risks, you could look into using the Cache object, as explained here.

For example:

protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    var products = Cache["products"] as DataTable;
    if(products == null)
    {
       products = obj_Dataset.Tables["Products"];
       Cache.Insert("products", products , null, DateTime.Now.AddHours(6), TimeSpan.Zero);
    }

    RadGrid1.DataSource = products;
}

There are a few things you need to know about this approach. First, Cache is global to the application, so the data stored in Cache must be the same for every user. Second, Cache will not automatically update itself, so if you update your data, you must explicitly update your Cache in code. If you have data that frequently changes, Cache is not a good place for it. Third, Cache takes up space in memory, so if you store large data sets in it, your application will suffer.

1
  • You are right about pagging and its so good when we have a lot of records.but when the Grid page change the profiler show me a Tsql that i khow its fecheng all the data Instead of the current page record.i think it transfer all the data on page and then show the currnet pages record.(for each page profiler show me "Select * from Products" ) Apr 23, 2012 at 19:16
1

i use this stored procedure to solve the problem: Parameters fill by radgrid property:

     Create PROCEDURE  [dbo].[CastumPaging]
                    (
                       @SqlTableName nvarchar(100),
                       @OrderColumn nvarchar (30),
                       @Direction nvarchar(5),
                       @PageIndex int,
                       @PageSize int
                    )
                    AS
                    BEGIN
                    declare @SQLText nvarchar(500)

                     set @SQLText = 'SELECT * FROM (SELECT top 100 PERCENT * , ROW_NUMBER() 
                          OVER(ORDER BY '+@OrderColumn+' '+ @Direction + ' ) as RowNum 
                          FROM '+@SqlTableName+'  order by '+@OrderColumn+' '+@Direction +' )
                          as DerivedTableName WHERE RowNum BETWEEN '+
                          str(@PageIndex * @PageSize) +' AND'+ 
                          str((@PageIndex* @PageSize) +@PageSize -1) 

                    exec(@SQLText)
                    END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.