Solved

Paging Error in DataGrid - Invalid CurrentPageIndex value

Posted on 2004-10-25
3,857 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
Question by:clintnash
    14 Comments
     
    LVL 3

    Expert Comment

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

    Author Comment

    by:clintnash
    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
    Can you provide some exception text?
    0
     
    LVL 1

    Author Comment

    by:clintnash
    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
    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
    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
    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
    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
    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
    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
    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
    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
    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:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now