Take the 2-minute tour ×

I have a gridview which gets populated in the backend code. I am trying to implement paging now, but when I am trying my way, I am getting nothing. Here is my piece of code:

public void generateTable()
    conn.ConnectionString = connString;
    SqlCommand comm = new SqlCommand("ViewBusinessInfo", conn);
    comm.CommandType = CommandType.StoredProcedure;
    comm.CommandTimeout = 2;
        SqlDataReader rdr = comm.ExecuteReader();

        if (rdr.HasRows)
            gvAssociation.DataSource = rdr;
            gvAssociation.AllowPaging = true;
            gvAssociation.PageSize = 10;
            lblResult.Text = "No businesses found.";
            lblResult.Visible = true;


Can anyone advice what am I doing wrong and I can't get the paging in the gridview? Thx in advance, Laziale

share|improve this question
For starters, you can probably change catch to catch(Exception ex) so you can see if you are actually getting an exception. An empty catch block will hide your exception. –  Bryan Crosby Nov 21 '11 at 17:25
add comment

3 Answers

up vote 1 down vote accepted

You cannot use paging with a DataReader. You should fill your data into a Dataset or a Datatable using a DataAdapter. Something like this:

 SqlCommand myCommand = new SqlCommand("ViewBusinessInfo", conn);
 SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand))
 DataTable dt = new DataTable();
share|improve this answer
add comment

The allowPaging and pagesize property of the gridview can be added in the .aspx, where the gridview tag is present.

<asp:GridView ID="gridView" OnPageIndexChanging="gridView_PageIndexChanging"  AllowPaging="True" pagesize="10" runat="server" />

Additionally, to make the paging links work, you have to add the following code in the gridview_PageIndexChanging event of the gridview:

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
   gridView.PageIndex = e.NewPageIndex;

Hope this is helpful.

share|improve this answer
that was not the problem. The problem was that you cannot use a DataReader to populate a GridView if paging is enabled. –  slfan Nov 22 '11 at 12:51
Yes you are right...it just skipped my mind. With DATAREADER as DATASOURCE, SERVER SIDE PAGING IS NOT ALLOWED. One will have to use DATAADAPTER-DATASET –  Aditya Nov 23 '11 at 10:44
add comment

Set the AllowPaging and PageSize declaratively, or do so before you call DataBind().

share|improve this answer
I am getting an error: The data source does not support server-side data paging –  Laziale Nov 21 '11 at 17:44
forums.asp.net/p/1320427/2623013.aspx should help. –  drdwilcox Nov 21 '11 at 17:55
add comment

Your Answer


