Link to home
Start Free TrialLog in
Avatar of clintnash
clintnashFlag for United States of America

asked on

Paging Error in DataGrid - Invalid CurrentPageIndex value

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
Avatar of nitrogenx
nitrogenx

Are you using custom paging?
Avatar of clintnash

ASKER

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...
Can you provide some exception text?
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

 
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
 
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
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
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
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.

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
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
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
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.
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

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