[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Paging Error in DataGrid - Invalid CurrentPageIndex value

Posted on 2004-10-25
14
Medium Priority
?
3,880 Views
Last Modified: 2012-08-14
I have a single datagrid on a webform, when I click  the page forward button I get the Invalid CurrentPageIndex error.  

The datasource for the datagrid is a stored procedure. In the datagrid I have set allow paging and allow custom paging (the latter was turned on at the nagging of the datagrid).  Based on my personal level of irritation with something that should be simple, I have set the point level at 250.  The following is the code in play

 Private Sub init_grid()

        Dim status As String = "sub"
        Dim counter As Integer = 10

        Dim dr1 As SqlDataReader
        dr1 = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "sp_pagecount", CUSTOMER_ID, status)
        If dr1.HasRows() Then
            dr1.Read()
            counter = dr1.Item("headcount")
        End If

        DataGrid1.VirtualItemCount = counter

        Dim dr As SqlDataReader
        dr = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "sp_fill_header", CUSTOMER_ID, status)

        DataGrid1.DataSource = dr
        DataGrid1.DataKeyField = "headerid"
        DataGrid1.DataBind()
    End Sub

    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
       
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        init_grid()

    End Sub
0
Comment
Question by:clintnash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12406330
Are you using custom paging?
0
 
LVL 1

Author Comment

by:clintnash
ID: 12406582
Yes, With Custom Paging turned off, I get the following error message.

AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID DataGrid1 when AllowPaging is set to true and the selected datasource does not implement ICollection.

Hope that helps,

Thanks,
Clint...
0
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12406640
Can you provide some exception text?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:clintnash
ID: 12406809
Sorry, it wasn't clear under which scenario that you wanted the exception text, so I will post both.  This is the exception text without custom paging.

AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID DataGrid1 when AllowPaging is set to true and the selected datasource does not implement ICollection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID DataGrid1 when AllowPaging is set to true and the selected datasource does not implement ICollection.

Source Error:


Line 66:         DataGrid1.DataSource = dr
Line 67:         DataGrid1.DataKeyField = "headerid"
Line 68:         DataGrid1.DataBind()
Line 69:     End Sub
Line 70:
 

Source File: c:\inetpub\wwwroot\Amerisource\Internal\TL_Pending.aspx.vb    Line: 68

Stack Trace:


[HttpException (0x80004005): AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID DataGrid1 when AllowPaging is set to true and the selected datasource does not implement ICollection.]
   System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
   System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
   System.Web.UI.WebControls.BaseDataList.DataBind()
   Amerisource.TL_Pending.init_grid() in c:\inetpub\wwwroot\Amerisource\Internal\TL_Pending.aspx.vb:68
   Amerisource.TL_Pending.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\Amerisource\Internal\TL_Pending.aspx.vb:35
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +750

 
0
 
LVL 1

Author Comment

by:clintnash
ID: 12406822
This is the exception text with custom paging set to true -
Server Error in '/Amerisource' Application.
--------------------------------------------------------------------------------

Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.

Source Error:


Line 66:         DataGrid1.DataSource = dr
Line 67:         DataGrid1.DataKeyField = "headerid"
Line 68:         DataGrid1.DataBind()
Line 69:     End Sub
Line 70:
 

Source File: c:\inetpub\wwwroot\Amerisource\Internal\TL_Pending.aspx.vb    Line: 68

Stack Trace:


[HttpException (0x80004005): Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.]
   System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
   System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
   System.Web.UI.WebControls.BaseDataList.DataBind()
   Amerisource.TL_Pending.init_grid() in c:\inetpub\wwwroot\Amerisource\Internal\TL_Pending.aspx.vb:68
   Amerisource.TL_Pending.DataGrid1_PageIndexChanged(Object source, DataGridPageChangedEventArgs e) in c:\inetpub\wwwroot\Amerisource\Internal\TL_Pending.aspx.vb:73
   System.Web.UI.WebControls.DataGrid.OnPageIndexChanged(DataGridPageChangedEventArgs e)
   System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() +1292
 
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12408323
Hi clintnash,

have you check what is the value returned in the counter variable
DataGrid1.VirtualItemCount = counter

as i read the mMSDN the property VirtualItemCount is the number of items not the nubmer of pages
so what is actually your stored procedure sp_pagecount do ?

Regards!
B..M
0
 
LVL 1

Author Comment

by:clintnash
ID: 12411822
sp_pagecount returns the number of items that will be returned to the grid. The text of the proc is below.  The proc returns:

headcount
------------
23


Proc ----

CREATE PROCEDURE sp_PageCount
      (
            @customerid int,
            @status varchar(5)
      )

AS

SET NOCOUNT ON

select count(*) as headcount
from header
where custid = @customerid and
status = @status


GO
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12411906
and what is the PageSize of your datagrid and which page number you click on to see this error ?

aslo how many pages the datagrid show you that you have ?
B..M
0
 
LVL 1

Author Comment

by:clintnash
ID: 12412001
The pagesize is set to 4.  The error is generated on clicking numbers 2 - 4 on the grid. The datagrid shows that I have 4 pages (which for the test client sp_pagecount returns 13), so that at least seems to be working okay.

0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12412110
also, how do you bind your datagrid in page_load ?
do you set it in
If Not IsPostBack Then
  bind grid
End If

because if you don't do that the effect will this you receive

B..M
0
 
LVL 1

Author Comment

by:clintnash
ID: 12412176
I call init_grid when the page postback which does databind the datagrid as its last step.  Currently I am loading the grid using a datareader, which apprarently doesn't support ICollection, which requires that I turn on custom paging. At this point, should I change my datareader to a dataset and turn off custom paging?  Code below:

if not page.ispostback then
     init_grid
end if

Private Sub init_grid()

        Dim status As String = "sub"
        Dim counter As Integer = 10

        Dim dr1 As SqlDataReader
        dr1 = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "sp_pagecount", CUSTOMER_ID, status)
        If dr1.HasRows() Then
            dr1.Read()
            counter = dr1.Item("headcount")
        End If

        DataGrid1.VirtualItemCount = counter

        Dim dr As SqlDataReader
        dr = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "sp_fill_header", CUSTOMER_ID, status)
               
        DataGrid1.DataSource = dr
        DataGrid1.DataKeyField = "headerid"
        DataGrid1.DataBind()
   
 End Sub
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12414081
you can try with dataset and see what will be the result - if it fits to your requirements
also, why do you use custompagging ?

B..M
0
 
LVL 1

Author Comment

by:clintnash
ID: 12414165
When you use a datareader (or any datasource that doesn't support ICollection) as a datasource the datagrid requires the use of custom paging.  To work around my problem, I have changed the datasource to a dataset, stored my ds object in a session variable and set the datagrid datasource = session(dataset) before calling databind in the PageIndexChanged event.

While this has fixed my problem for the time being, it doesn't answer the primary question of this post and that is - how using a datareader as a datasource do I get paging to work.  

Just for fun since this seems to be a little more difficult than originally thought, I am raising the point value to 350.
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 1400 total points
ID: 12416531
so, here is the problem:
when you do the custom pagging .net framework does not page by your self , e.g. in normal paging you just set the index of the paging and bind the grid and .net framework shows you the needed records
in custom pagging it is not like that you have to retrieve only the records that you want to show

i've change your code a little ( tested with different values, database, etc )
look at this article  - it describes you the best: http://bdn.borland.com/article/images/30306/CustomPagingWF.aspx.cs.html.html

what you have to modified is to create parameters in your stored procedures

Regards!
B..M
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

650 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