Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used gridview paging and fill from database

each page has 10 data

for ex 1 2 3 each page 10 data there is no problem

1 and 2 has 10 data 3 third page 3 data
I click 3 third page and 2 second page so before

this error gives me

SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


so each page has 10 data there is no problem but if last page less then 10 data click last page and click before page this error gives me

my code this:

C#
MySqlConnection conn = new MySqlConnection(connect.connStr);
                    conn.Open();
                    string sql = "select * from xxx where yyy='" + zz+ "'";
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    MySqlDataAdapter Adapter = new MySqlDataAdapter(cmd);
                    DataTable daset = new DataTable();
                    Adapter.Fill(daset);
                    GridView1.DataSource = daset;
                    GridView1.DataBind();

                    Cache["xxx"]= daset;

                    conn.Close(); 

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
       {

           GridView1.PageIndex = e.NewPageIndex;
           gridbinding();
       }

       private void gridbinding()
       {
           GridView1.DataSource = (DataTable) Cache["xxx"];
           GridView1.DataBind();

       }
Posted
Updated 23-Apr-12 22:43pm
v2

1 solution

There is difference between two events of pagination that is exposed for GridView:
MSDN: GridView.PageIndexChanged Event[^] and MSDN: GridView.PageIndexChanging Event[^]

You can read the difference, you need to use PageIndexChanged event and everything should work fine. Sample codes for both the events are also provided in the links shared above.
 
Share this answer
 
Comments
VJ Reddy 24-Apr-12 8:00am    
Good answer. 5!
Sandeep Mewara 24-Apr-12 8:38am    
Thanks.
Member-2338430 24-Apr-12 9:03am    
ı found problem but ı dont understand why run like this

gridview has 2event gridview row command and page index changing
I click page row command running so there is no row null comes back
Member-2338430 25-Apr-12 8:27am    
this is not good answer this is not a good behavior I dont want to any idea for this question vjreddy or Sandeep or anybody

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900