?
Solved

index out of range

Posted on 2005-05-06
3
Medium Priority
?
216 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:ramrod1979
  • 2
2 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 13944260
/// 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
 
LVL 23

Accepted Solution

by:
b1xml2 earned 1000 total points
ID: 13944278
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question