Exception regarding the paging property of a GridView

Today I found a solution for the following exception:

“The data source does not support server-side data paging.”

This happens when I set the ASP.NET GridView’s paging property to true and data source was bounded with a data reader.

Exception was thrown because you cannot use the paging feature when the grid is bounded to a data reader.

To work around this problem, we can simply create an data adapter and an empty data set. Then, use the data adapter to fill the dataset and set the gridview’s data source to the dataset. Dataset allows server side paging.

Code example:

string sql = @"SELECT ua.Username, ua.First_Name, ua.Surname, ua.Active,at.Description
    FROM USER_ACCOUNT ua, Account_Type at 
    WHERE ua.Account_Type_ID = at.Account_Type_ID";
cmd.CommandText = sql;
OleDbDataAdapter da = new OleDbDataAdapter(cmd); //create a data adapter with an OledbCommand
DataSet ds = new DataSet(); //constructs an empty dataset
da.Fill(ds); //fill the dataset with the adapter
//OleDbDataReader dr = cmd.ExecuteReader();
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
this.GridView1.AllowPaging = true; //allow paging does not work with datareader
//dr.Close();

In the code above, the section where was commented out is the code that caused the exception. Note how I replace it with the data set easily.


Leave a Reply

Your email address will not be published. Required fields are marked *