0

I have some basic c# code which connects to a database and prints out all rows returned i.e:

using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
{
    using (command = new SqlCommand("select * from table1", connection))
    {
        connection.Open();

        using (reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Div1.InnerHtml += reader["col1"].ToString() + "<br />";
            }
        }
    }
}

How do I add pagination to this?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

2 Answers2

2

you can use Gridview control. it is more easier, rather than building your own pagination method.

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False"  allowpaging="True">
    <Columns>
     //your all boundfield columns 
    </Columns>
</asp:gridView>

for Paging, you can use OnPageIndexChanging event handler. Page Index Changing.

Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
0

AFAIK with DataReader, you will need to "Skip" (PageNumber - 1) * PageSize rows by just looping reader.Read().

As an alternative, you could do the pagination in your SQL query - this way you return only the records that you need (although you will need to do additional work to determine the total number of records that match the filter)

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285