The data source does not support server-side data paging

C#asp.netLinqSortingGridview

C# Problem Overview


I have a GridView on my screen and need it to allow paging.

Markup:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
  AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
  <Columns>
    <asp:BoundField DataField="appID" HeaderText="appID" SortExpression="appID" />
  </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
  SelectMethod="GetBookingId" 
  TypeName="AppointmentRepository">
  <SelectParameters>
    <asp:Parameter Name="maximumRows" Type="Int32" />
    <asp:Parameter Name="startRowIndex" Type="Int32" />
  </SelectParameters>
</asp:ObjectDataSource>

Code-behind:

ObjectDataSource1.SelectParameters["maximumRows"].DefaultValue = "10";
ObjectDataSource1.SelectParameters["startRowIndex"].DefaultValue = "0";

LINQ query:

public IQueryable<tblAppointment> GetBookingId(int maximumRows, int startRowIndex)
{
    var result = (FROM a IN dc.tblAppointments
                  SELECT a).Skip(startRowIndex).Take(maximumRows);
}

However I receive this error: >The data source does not support server-side data paging.

What am I doing wrong?

C# Solutions


Solution 1 - C#

A simple ToList() on your result var should work.

Edit: As explained in comments below my answer, the reason for the error is that the data source should implement ICollection. IEnumerable does not, when you do ToList() it converts it into a list which implements ICollection.

Solution 2 - C#

You can use generic List<T> also. See the sample code snippet:

public List<Company> GetContactList(int startindex)
{
   
    string path = Server.MapPath("~/contacts.xml");
    XDocument xd = XDocument.Load(path);
    IEnumerable<Company> results = (from items in xd.Elements("Company").Elements("Contact")
                   select new Company
                   {
                       Id = items.Element("ID").Value,
                       Photo = (string)items.Element("photo").Value,
                       Name = (string)items.Element("Name").Value,
                       BloodGroup = (string)items.Element("Bg").Value,
                       Dob = (string)items.Element("dob").Value,
                       Anniversery = (string)items.Element("avd").Value,
                       Mobile = (string)items.Element("cnum").Value,
                       designation = (string)items.Element("desig").Value,
                       Team = (string)items.Element("team").Value
                   }).Skip(startindex*10).Take(10);
    return (List<Company>) results;
}

You can also use DataSet/DataTable instead of DataReader.

Solution 3 - C#

.ToList() at the end of the DataSource, I am assigning worked for me like below:

gvCaseLabelsLeft.DataSource = caseLabelsList.OrderBy(c=>c.caseLabelNumber).ToList();

Solution 4 - C#

I've changed my code to this:

public List<string> ListofNewsTitle()
{
    var query = from n in db.NewsEvents
                orderby n.NewsDate descending
                select n.NewsTitle;
    return query.ToList();        
}

Solution 5 - C#

In ObjectDataSource just add enablePaging="true" that will work.

Solution 6 - C#

LINQ query:

ProductController.cs:

List<Product> products= productModel.GetProducts(start, offset);

ProductModel.cs:

public List<Product> GetProducts(int start, int offset)
{
    IEnumerable<Product> query = from m in db.Products
                                 orderby m.Id descending
                                 select m;
    query = query.Skip(start).Take(offset);
    return query.ToList();
}

Solution 7 - C#

If You are Using SqldataReader then its not support Paging.

Solution to this error is making use of DataSources such as Generic List collections, DataTables, DataSets, etc. to bind the GridView.

Solution 8 - C#

Try this article https://www.aspsnippets.com/Articles/ASPNet-GridView-The-data-source-does-not-support-server-side-data-paging.aspx

in summary try to use these lines

private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, ContactName, Country FROM Customers"))
        {
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                GridView1.DataSource = sdr;
                GridView1.DataBind();
            }
            con.Close();
        }
    }
}
 
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPaging">
<Columns>
    <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
    <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
    <asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionClareBearView Question on Stackoverflow
Solution 1 - C#almog.oriView Answer on Stackoverflow
Solution 2 - C#renjucoolView Answer on Stackoverflow
Solution 3 - C#sakhyaView Answer on Stackoverflow
Solution 4 - C#Data_ScientistsView Answer on Stackoverflow
Solution 5 - C#VporechaView Answer on Stackoverflow
Solution 6 - C#Shibu ThomasView Answer on Stackoverflow
Solution 7 - C#Akhil SinghView Answer on Stackoverflow
Solution 8 - C#Aljohn YamaroView Answer on Stackoverflow