ClareBear
ClareBear

Reputation: 1523

The data source does not support server-side data paging

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?

Upvotes: 59

Views: 66554

Answers (8)

almog.ori
almog.ori

Reputation: 7889

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.

Upvotes: 135

Aljohn Yamaro
Aljohn Yamaro

Reputation: 2881

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>

Upvotes: 0

Akhil Singh
Akhil Singh

Reputation: 730

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.

Upvotes: 2

Shibu Thomas
Shibu Thomas

Reputation: 3196

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();
}

Upvotes: 0

Vporecha
Vporecha

Reputation: 73

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

Upvotes: 0

sakhya
sakhya

Reputation: 21

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

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

Upvotes: 2

Mehdi
Mehdi

Reputation: 19

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();        
}

Upvotes: 1

renjucool
renjucool

Reputation: 312

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.

Upvotes: 3

Related Questions