Link to home
Start Free TrialLog in
Avatar of ramrod1979
ramrod1979

asked on

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?
Avatar of b1xml2
b1xml2
Flag of Australia image

/// 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();

}
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial