clintnash
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(Co nfiguratio nSettings. AppSetting s(Web.Glob al.CfgKeyC onnString) , "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(Co nfiguratio nSettings. AppSetting s(Web.Glob al.CfgKeyC onnString) , "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. DataGridPa geChangedE ventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
init_grid()
End Sub
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(Co
If dr1.HasRows() Then
dr1.Read()
counter = dr1.Item("headcount")
End If
DataGrid1.VirtualItemCount
Dim dr As SqlDataReader
dr = SqlHelper.ExecuteReader(Co
DataGrid1.DataSource = dr
DataGrid1.DataKeyField = "headerid"
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_PageIndexChanged
DataGrid1.CurrentPageIndex
init_grid()
End Sub
Are you using custom paging?
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...
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?
ASKER
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\Ameriso urce\Inter nal\TL_Pen ding.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.C reateContr olHierarch y(Boolean useDataSource)
System.Web.UI.WebControls. BaseDataLi st.OnDataB inding(Eve ntArgs e)
System.Web.UI.WebControls. BaseDataLi st.DataBin d()
Amerisource.TL_Pending.ini t_grid() in c:\inetpub\wwwroot\Ameriso urce\Inter nal\TL_Pen ding.aspx. vb:68
Amerisource.TL_Pending.Pag e_Load(Obj ect sender, EventArgs e) in c:\inetpub\wwwroot\Ameriso urce\Inter nal\TL_Pen ding.aspx. vb:35
System.Web.UI.Control.OnLo ad(EventAr gs e) +67
System.Web.UI.Control.Load Recursive( ) +35
System.Web.UI.Page.Process RequestMai n() +750
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\Ameriso
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.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
Amerisource.TL_Pending.ini
Amerisource.TL_Pending.Pag
System.Web.UI.Control.OnLo
System.Web.UI.Control.Load
System.Web.UI.Page.Process
ASKER
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\Ameriso urce\Inter nal\TL_Pen ding.aspx. vb Line: 68
Stack Trace:
[HttpException (0x80004005): Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.]
System.Web.UI.WebControls. DataGrid.C reateContr olHierarch y(Boolean useDataSource)
System.Web.UI.WebControls. BaseDataLi st.OnDataB inding(Eve ntArgs e)
System.Web.UI.WebControls. BaseDataLi st.DataBin d()
Amerisource.TL_Pending.ini t_grid() in c:\inetpub\wwwroot\Ameriso urce\Inter nal\TL_Pen ding.aspx. vb:68
Amerisource.TL_Pending.Dat aGrid1_Pag eIndexChan ged(Object source, DataGridPageChangedEventAr gs e) in c:\inetpub\wwwroot\Ameriso urce\Inter nal\TL_Pen ding.aspx. vb:73
System.Web.UI.WebControls. DataGrid.O nPageIndex Changed(Da taGridPage ChangedEve ntArgs e)
System.Web.UI.WebControls. DataGrid.O nBubbleEve nt(Object source, EventArgs e)
System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args)
System.Web.UI.WebControls. DataGridIt em.OnBubbl eEvent(Obj ect source, EventArgs e)
System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args)
System.Web.UI.WebControls. LinkButton .OnCommand (CommandEv entArgs e)
System.Web.UI.WebControls. LinkButton .System.We b.UI.IPost BackEventH andler.Rai sePostBack Event(Stri ng eventArgument)
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
System.Web.UI.Page.Process RequestMai n() +1292
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\Ameriso
Stack Trace:
[HttpException (0x80004005): Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.]
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
Amerisource.TL_Pending.ini
Amerisource.TL_Pending.Dat
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
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
have you check what is the value returned in the counter variable
DataGrid1.VirtualItemCount
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
ASKER
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
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
aslo how many pages the datagrid show you that you have ?
B..M
ASKER
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
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
ASKER
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(Co nfiguratio nSettings. AppSetting s(Web.Glob al.CfgKeyC onnString) , "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(Co nfiguratio nSettings. AppSetting s(Web.Glob al.CfgKeyC onnString) , "sp_fill_header", CUSTOMER_ID, status)
DataGrid1.DataSource = dr
DataGrid1.DataKeyField = "headerid"
DataGrid1.DataBind()
End Sub
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(Co
If dr1.HasRows() Then
dr1.Read()
counter = dr1.Item("headcount")
End If
DataGrid1.VirtualItemCount
Dim dr As SqlDataReader
dr = SqlHelper.ExecuteReader(Co
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
also, why do you use custompagging ?
B..M
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.