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
LVL 1
clintnashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nitrogenxCommented:
Are you using custom paging?
0
clintnashAuthor Commented:
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
nitrogenxCommented:
Can you provide some exception text?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

clintnashAuthor Commented:
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
clintnashAuthor Commented:
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
mmarinovCommented:
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
clintnashAuthor Commented:
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
mmarinovCommented:
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
clintnashAuthor Commented:
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
mmarinovCommented:
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
clintnashAuthor Commented:
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
mmarinovCommented:
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
clintnashAuthor Commented:
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
mmarinovCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

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.