Invalid CurrentPageIndex error

Discussion in 'ASP .Net Datagrid Control' started by Spike, May 3, 2004.

  1. Spike

    Spike Guest

    Hi all,
    Invalid CurrentPageIndex error seems to occur even when
    CurrentPageIndex is set to zero just before binding the
    datagrid. Can anyone provide any more insights on this
    problem. Thanks in advance.
     
    Spike, May 3, 2004
    #1
    1. Advertisements

  2. Alvin Bruney [MVP], May 3, 2004
    #2
    1. Advertisements

  3. Spike

    Spike Guest

    I have read the article and tried handling the exception
    but I still get the error. I can mail you the code if
    required.
     
    Spike, May 3, 2004
    #3
  4. post the relevant portions of the code

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
     
    Alvin Bruney [MVP], May 3, 2004
    #4
  5. Spike

    Spike Guest

    Here is the WebForm
    -------------------------------
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="WebForm1.aspx.vb"
    Inherits="DataGridPageProblem.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta content="Microsoft Visual Studio.NET
    7.0" name="GENERATOR">
    <meta content="Visual Basic 7.0"
    name="CODE_LANGUAGE">
    <meta content="JavaScript"
    name="vs_defaultClientScript">
    <meta
    content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post"
    runat="server">
    <asp:DataGrid id="DataGrid1"
    runat="server" PageSize="10" Height="0px" Width="100%"
    AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" CellPadding="0">
    <SelectedItemStyle Font-
    Bold="True" ForeColor="#663399"
    BackColor="#FFCC66"></SelectedItemStyle>
    <ItemStyle
    ForeColor="#330099" BackColor="White"></ItemStyle>
    <HeaderStyle Font-
    Bold="True" ForeColor="#FFFFCC"
    BackColor="#990000"></HeaderStyle>
    <Columns>
    <ASP:BoundColumn
    DataField="LastName" SortExpression="LastName"
    HeaderText="Last Name"></ASP:BoundColumn>
    <ASP:BoundColumn
    DataField="Title" SortExpression="Title"
    HeaderText="Title" Visible="False"></ASP:BoundColumn>
    <ASP:BoundColumn
    DataField="FirstName" SortExpression="FirstName"
    HeaderText="First Name"></ASP:BoundColumn>
    </Columns>
    <PagerStyle Font-
    Bold="True" HorizontalAlign="Right" ForeColor="#330099"
    BackColor="#FFFFCC" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid><BR>
    </form>
    </FORM></FORM>
    </body>
    </HTML>
    ---------------------------------------
    Here is the code behind
    ----------------------------
    Imports System.Data
    Imports System.Data.SqlClient

    Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents DataGrid1 As
    System.Web.UI.WebControls.DataGrid

    Private Const mstrAlphaPageCommand = "AlphaPage"
    Private mstrAlphaPageList As String
    = "A,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
    Private mConnectionString As String = "Password=;User
    ID=sa;Data Source=<<datasource>>;Initial Catalog=Northwind"

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web
    Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Page.IsPostBack Then
    BindGrid()
    End If
    End Sub

    Sub BindGrid(Optional ByVal alpha As String = "")
    Dim cnn As New SqlConnection(mConnectionString)
    Dim da As SqlDataAdapter
    If alpha = "" Then
    'da = New SqlDataAdapter("select
    title,lastname,firstname from employees where lastname
    like 'D%'", cnn)
    da = New SqlDataAdapter("select
    title,lastname,firstname from employees", cnn)
    Else
    da = New SqlDataAdapter("select
    title,lastname,firstname from employees where lastname
    like '" & LCase(alpha) & "%'", cnn)
    End If
    Dim ds As New DataSet()
    da.Fill(ds, "employees")
    DataGrid1.DataSource = ds
    DataGrid1.CurrentPageIndex = 0
    DataGrid1.DataBind()
    End Sub

    Private Sub DataGrid1_SelectedIndexChanged(ByVal
    sender As System.Object, ByVal e As System.EventArgs)
    Handles DataGrid1.SelectedIndexChanged

    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
    BindGrid()
    End Sub

    Private Sub DataGrid1_ItemCommand(ByVal source As
    System.Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs)
    Handles DataGrid1.ItemCommand
    If e.CommandName = mstrAlphaPageCommand Then
    DataGrid1.PageSize = System.Int32.MaxValue
    DataGrid1.CurrentPageIndex = 0
    Try
    BindGrid(e.CommandArgument)
    Catch excp As HttpException
    DataGrid1.CurrentPageIndex = 0
    BindGrid(e.CommandArgument)
    End Try
    End If
    End Sub

    Private Function GetPagingRow(ByVal objItem As
    DataGridItem) As Table
    Dim intColspan As Integer = objItem.Cells.Count
    Dim tcNumPageRow As New TableRow()
    tcNumPageRow.HorizontalAlign =
    DataGrid1.PagerStyle.HorizontalAlign
    Dim tcNumPageCell As TableCell
    For Each tcNumPageCell In objItem.Cells
    tcNumPageRow.Cells.Add(tcNumPageCell)
    Next
    Dim tcAlphaPageRow As New TableRow()
    tcAlphaPageRow.HorizontalAlign =
    DataGrid1.PagerStyle.HorizontalAlign
    Dim tcAlphaPageCell As New TableCell()
    tcAlphaPageCell.ColumnSpan = intColspan
    Dim i As Integer
    For i = 65 To 65 + 25
    If mstrAlphaPageList.IndexOf(Chr(i)) <> -1 Then
    Dim l As New LinkButton()
    Dim lc As New LiteralControl()
    lc.Text = " "
    l.Text = Chr(i)
    l.CommandName = mstrAlphaPageCommand
    l.CommandArgument = Chr(i)
    tcAlphaPageCell.Controls.Add(l)
    tcAlphaPageCell.Controls.Add(lc)
    Else
    Dim lbl As New Label()
    lbl.Text = Chr(i) & " "
    tcAlphaPageCell.Controls.Add(lbl)
    End If
    Next
    tcAlphaPageRow.Cells.Add(tcAlphaPageCell)
    Dim tcTable As New Table()
    tcTable.Width =
    System.Web.UI.WebControls.Unit.Percentage(100)
    tcTable.Rows.Add(tcNumPageRow)
    tcTable.Rows.Add(tcAlphaPageRow)
    Return tcTable
    End Function

    Private Sub DataGrid1_ItemCreated(ByVal sender As
    Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    DataGrid1.ItemCreated
    If e.Item.ItemType = ListItemType.Pager Then
    Dim tcPageTable As Table = GetPagingRow(e.Item)
    Dim tcPageCell As New TableCell()
    tcPageCell.ColumnSpan = DataGrid1.Columns.Count
    tcPageCell.Controls.Add(tcPageTable)
    e.Item.Cells.Clear()
    e.Item.Cells.Add(tcPageCell)
    End If
    End Sub
    End Class
    ------------------------------------------------
     
    Spike, May 3, 2004
    #5
  6. try this instead,
    currentpageindex
    datasource
    databind

    can you reproduce this error at will?

    If you can, CTRL +ALT + E, choose break into the debugger, ok. Reproduce the
    problem. Point me to the line of code which fires the exception.
     
    Alvin Bruney [MVP], May 3, 2004
    #6
  7. Spike

    Spike Guest

    The error occurs only when the dataset returns more than 1
    row. It occurs when I bind the grid in the ItemCommand
    event i.e.
    BindGrid(e.CommandArgument)
     
    Spike, May 4, 2004
    #7
  8. DataGrid1.PageSize = System.Int32.MaxValue

    This is one problem since you are setting the size to be larger than the
    collection. If you want to remove paging, set DataGrid1.AllowPaging = false

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
     
    Alvin Bruney [MVP], May 4, 2004
    #8
  9. Spike

    Spike Guest

    Yes that is the problem. But it fails only if I set
    Pagesize to System.Int32.MaxValue. I tried setting
    Pagesize to 10000000 and it worked fine. Seems strange
    that Pagesize cannot be set to max integer value.
     
    Spike, May 5, 2004
    #9
  10. would you mind reporting this as a bug? here's a link.
    http://support.microsoft.com/default.aspx?scid=fh;[ln];oasoem

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
     
    Alvin Bruney [MVP], May 5, 2004
    #10
  11. write up a complete report and post it here. I'll route it to product
    support. Sorry, I should have offered you this option in the first place.

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
     
    Alvin Bruney [MVP], May 6, 2004
    #11
  12. Spike

    Spike Guest

    Well here's the report.
    -----------------------------
    Setting PageSize property of DataGrid to
    System.Integer.MaxValue gives the following error

    Invalid CurrentPageIndex value. It must be >= 0 and < the
    PageCount.

    Steps to reproduce the error
    1. Create a new ASP.Net Web project.
    2. Add a WebForm to the project.
    3. Add a DataGird (DataGrid1) to the WebForm.
    4. Enable Paging for the datagrid.
    5. Add atleast 1 ButtonColumn to the DataGrid
    6. Write code to bind the DataGrid to any datasource.
    7. Trap the ItemCommand Event for the Button Column.
    8. In the ItemCommand event add the following code
    DataGrid1.Pagesize = System.Int32.MaxValue.
    9. Run the application.
    10. Clicking on the ButtonColumn should give the error.
     
    Spike, May 7, 2004
    #12
  13. Spike

    Spike Guest

    Oops I seem to have missed out some important info in the
    report. Heres the updated report
    -----------------------------
    Setting PageSize property of DataGrid to
    System.Integer.MaxValue gives the following error

    Invalid CurrentPageIndex value. It must be >= 0 and < the
    PageCount.

    Steps to reproduce the error
    1. Create a new ASP.Net Web project.
    2. Add a WebForm to the project.
    3. Add a DataGird (DataGrid1) to the WebForm.
    4. Enable Paging for the datagrid.
    5. Add atleast 1 ButtonColumn to the DataGrid
    6. Write code to bind the DataGrid to any datasource.
    7. Trap the ItemCommand Event for the Button Column.
    8. In the ItemCommand event add the following code
    DataGrid1.Pagesize = System.Int32.MaxValue.
    Code to bind the datagrid to datasource.
    9. Run the application.
    10. Clicking on the ButtonColumn should give the error.
    ---------------------------------------------
     
    Spike, May 7, 2004
    #13
  14. thanks, i'll test and submit.

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
     
    Alvin Bruney [MVP], May 7, 2004
    #14
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.