This is a migrated thread and some comments may be shown as answers.

RadGrid (or LinqDataSource) querying whole table with allowpaging

1 Answer 143 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sem
Top achievements
Rank 1
Sem asked on 22 Nov 2010, 04:59 PM
(Working in VS 2010, C# 4.0, RadControls for ASP.NET AJAX 2010.2.929.40, Sql Server 2008)
When I have the following radgrid :
<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" AllowPaging="True"
        AllowSorting="True" GridLines="None" OnNeedDataSource="RadGrid1_NeedDataSource"
        AutoGenerateColumns="true" AutoGenerateEditColumn="true">
</telerik:RadGrid>

and this in my code behind :

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    LinqDataSource ds = new LinqDataSource();
    ds.Selecting += (sd, evt) => evt.Result = new UserManager().GetAllUsers();
    RadGrid1.DataSource = ds;
}
3 queries are generated :
SELECT COUNT(*) AS [value]
FROM [dbo].[Portal_Users] AS [t0]
  
SELECT [t1].[Id], [t1].[FirstName], [t1].[LastName], [t1].[Email], [t1].[LastLogin], [t1].[Created]
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY [t0].[Id], [t0].[FirstName], [t0].[LastName], [t0].[Email], [t0].[LastLogin], [t0].[Created]) AS [ROW_NUMBER], [t0].[Id], [t0].[FirstName], [t0].[LastName], [t0].[Email], [t0].[LastLogin], [t0].[Created]
    FROM [dbo].[Portal_Users] AS [t0]
    ) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
ORDER BY [t1].[ROW_NUMBER]
  
SELECT [t0].[Id], [t0].[FirstName], [t0].[LastName], [t0].[Email], [t0].[LastLogin], [t0].[Created]
FROM [dbo].[Portal_Users] AS [t0]

The first 2 queries are as expected but the 3rd one queries the whole users table. As this gives horrible performance with a large dataset I started digging around. Then I discovered that this :

<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" AllowPaging="True"
    AllowSorting="True" GridLines="None" DataSourceID="linqDS"
    AutoGenerateColumns="true" AutoGenerateEditColumn="true">
</telerik:RadGrid>
<asp:LinqDataSource ID="linqDS" OnSelecting="selecting" runat="server" />
and this in my codebehind :
protected void selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    e.Result = new UserManager().GetAllUsers();
}
only gives the first 2 queries of the aforementioned 3 which solves my problem.

However, I don't know why in the first case I get 3 queries instead of 2. I would expect only the 2 queries in both cases. More worrying, I can imagine forgetting (me or my colleagues) that I must declare the LinqDataSource in the aspx page and not in my codebehind and so end up querying the whole table.

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 25 Nov 2010, 01:21 PM
Hello Sem,

This is because when using the DataSourceID RadGrid knows it is binding to a data source control and applies a different set of databinding actions to benefit from the capabilities the data source control offers. This is not the case when setting the DataSource property of the control to the data source control instance. In any case, when using DataSource controls, the recommended approach is to pass them to the grid through the DataSourceID.

Veli
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Sem
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or