index out of range

Hi

I'm using paging on my datagrid but have run into the following problem, when I choose to go to the next page I get the following error: Specified argument was out of the range of valid values. Parameter name: index. I am using a viewstate variable that I'm passing to my bindgrid method in order to get the date range that I want to occupy in my datagrid. So my bindgrid method looks like the following:

private void bindGrid()
          {
               sqlConnection1.Open();
               sqlDataAdapter1.SelectCommand.CommandText="select * from mainTABLE where [date]='"
                    +ViewState["dateWorked"] +"'";
               sqlDataAdapter1.Fill(dataSet1);
               sqlDataAdapter2.Fill(agMain1);
               DataGrid1.DataBind();
               DataGrid1.DataSource=dataSet1;
               sqlConnection1.Close();

          }

I use two datasets because the datagrid has two drop down list that are occupied with selections from another table.

The page changed event is the standard one as follows:

private void changePage(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
          {
               DataGrid1.CurrentPageIndex = e.NewPageIndex;
               bindGrid();
               
          }

And the view selected date gets the date range to load into the bindgrid method from the calendar control as follows:

private void viewSelectedDate(object sender, System.EventArgs e)
          {
               ViewState["dateWorked"] = Calendar1.SelectedDate.ToLongDateString();
               bindGrid();
         
          }

What am I missing? What have I done wrong?
ramrod1979Asked:
Who is Participating?
 
b1xml2Commented:
what's happening is this:

scenario 1
======
1. You have selected date1.
2. You see the results, there are 7 pages returned
3. You click on page 5. (The CurrentPageIndex is now 4)
4. You now change the SelectedDate.
5. A rebind occurs (with the CurrentPageIndex still being at 4)
6. However this time, only 3 pages are returned.


The resolution is to pass the CurrentPageIndex value in the BindGrid method itself.
0
 
b1xml2Commented:
/// use this when bind without paging
private void BindGrid()
{
      BindGrid(0);
}

private void BindGrid(int index)
{
      sqlConnection1.Open();
      sqlDataAdapter1.SelectCommand.CommandText="select * from mainTABLE where [date]='"
          +ViewState["dateWorked"] +"'";
      sqlDataAdapter1.Fill(dataSet1);
      sqlDataAdapter2.Fill(agMain1);
      DataGrid1.CurrentPageIndex = index;
      DataGrid1.DataSource=dataSet1;
      DataGrid1.DataBind();
      sqlConnection1.Close();

}

private void changePage(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
      BindGrid(e.NewPageIndex);
}

private void viewSelectedDate(object sender, System.EventArgs e)
{
      ViewState["dateWorked"] = Calendar1.SelectedDate.ToLongDateString();
      BindGrid();

}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.