Pagination problem with asp.net 1.1 datagrid

Discussion in 'ASP .Net' started by Julia B, Sep 30, 2008.

  1. Julia B

    Julia B Guest

    Hi all

    Got a weird problem with pagination on a datagrid in asp.net 1.1.

    It's populated depending on user selected criteria (it either displays all
    or 1 record). It works fine in the following circumstances:

    Datagrid populated with 1 record then re-populated with all
    Datagrid populated with 1 record then re-populated with another single record
    Datagrid populated with multiple records and displaying page 1 then
    repopulated with 1 record

    It falls over in the following circumstance:

    Datagrid is populated with multiple records and displaying page 2 or 3 etc
    then user attempts to repopulate with 1 record.

    The error message is: Invalid CurrentPageIndex value. It must be >=0 and <
    the PageCount. I'm guessing it's something to do with the fact that the
    current page index is more than 0 and that confuses it, so I tried to reset
    the pagecount to 0 but got a build error that pagecount is a readonly
    property.

    Here's the code:

    Private Sub PartDataGrid_PageIndexChanged(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
    PartDataGrid.PageIndexChanged
    ‘This sub refreshes the datagrid if the page index is changed
    PartDataGrid.CurrentPageIndex = e.NewPageIndex
    If txtPartSearch.Text = "" Or txtPartSearch.Text = "(Enter part
    number)" Then
    showAllRecords()
    Else
    searchRecords()
    End If
    PartDataGrid.DataBind()
    End Sub

    Private Sub PartDataGrid_Page(ByVal sender As Object, ByVal e As
    DataGridPageChangedEventArgs)
    PartDataGrid.CurrentPageIndex = e.NewPageIndex
    PartDataGrid.DataBind()
    End Sub

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles cmdSearch.Click
    ‘This is what the user calls when pressing a search button to display 1 record
    PartDataGrid.CurrentPageIndex = 0
    End Sub

    Private Sub cmdAll_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles cmdAll.Click
    ‘This is what the user calls when pressing a show all records button
    PartDataGrid.CurrentPageIndex = 0
    PartDataSet1.Clear()
    strSQL = "SELECT * FROM PARTS"
    Dim PartDataAdapter As New OleDb.OleDbDataAdapter
    PartDataAdapter.SelectCommand = New OleDb.OleDbCommand(strSQL,
    PartDbConnection)
    PartDataAdapter.Fill(PartDataSet1, "Parts")
    Session("PartDataSet") = PartDataSet1
    PartDataGrid.DataBind()
    End Sub

    Private Sub searchRecords()
    PartDataSet1.Clear()
    Dim searchString As String = txtPartSearch.Text
    Now run the search
    strSQL = "Select * FROM Parts WHERE PartNo LIKE '" + searchString +
    "'"
    'now run the query using a data adapter
    Dim PartDataAdapter As New OleDb.OleDbDataAdapter
    PartDataAdapter.SelectCommand = New OleDb.OleDbCommand(strSQL,
    PartDbConnection)
    PartDataAdapter.Fill(PartDataSet1, "Parts")
    Session("PartDataSet") = PartDataSet1
    'This is where I get the error
    PartDataGrid.DataBind()
    End Sub

    Thanks for your help.
    Julia
    Julia B, Sep 30, 2008
    #1
    1. Advertising

  2. Julia B

    Julia B Guest

    RE: Update - Pagination problem with asp.net 1.1 datagrid

    My apologies, I simplified the code from original and made a mistake. Please
    see updated version below.

    "Julia B" wrote:

    > Hi all
    >
    > Got a weird problem with pagination on a datagrid in asp.net 1.1.
    >
    > It's populated depending on user selected criteria (it either displays all
    > or 1 record). It works fine in the following circumstances:
    >
    > Datagrid populated with 1 record then re-populated with all
    > Datagrid populated with 1 record then re-populated with another single record
    > Datagrid populated with multiple records and displaying page 1 then
    > repopulated with 1 record
    >
    > It falls over in the following circumstance:
    >
    > Datagrid is populated with multiple records and displaying page 2 or 3 etc
    > then user attempts to repopulate with 1 record.
    >
    > The error message is: Invalid CurrentPageIndex value. It must be >=0 and <
    > the PageCount. I'm guessing it's something to do with the fact that the
    > current page index is more than 0 and that confuses it, so I tried to reset
    > the pagecount to 0 but got a build error that pagecount is a readonly
    > property.
    >
    > Here's the code:
    >
    >Private Sub PartDataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles PartDataGrid.PageIndexChanged

    PartDataGrid.CurrentPageIndex = e.NewPageIndex
    If txtPartSearch.Text = "" Or txtPartSearch.Text = "(Enter part
    number)" Then
    showAllRecords()
    Else
    searchRecords()
    End If
    End Sub

    Private Sub PartDataGrid_Page(ByVal sender As Object, ByVal e As
    DataGridPageChangedEventArgs)
    PartDataGrid.CurrentPageIndex = e.NewPageIndex
    PartDataGrid.DataBind()
    End Sub

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles cmdSearch.Click
    ‘User clicks this button to select one record
    PartDataGrid.CurrentPageIndex = 0
    searchRecords()
    End Sub

    Private Sub cmdAll_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles cmdAll.Click
    ‘User clicks this button to select all records
    PartDataGrid.CurrentPageIndex = 0
    showAllRecords()
    End Sub

    Private Sub showAllRecords()
    PartDataSet1.Clear()
    strSQL = "SELECT * FROM PARTS"
    Dim PartDataAdapter As New OleDb.OleDbDataAdapter
    PartDataAdapter.SelectCommand = New OleDb.OleDbCommand(strSQL,
    PartDbConnection)
    PartDataAdapter.Fill(PartDataSet1, "Parts")
    Session("PartDataSet") = PartDataSet1
    PartDataGrid.DataBind()
    End Sub

    Private Sub searchRecords()
    PartDataSet1.Clear()
    Dim searchString As String = txtPartSearch.Text
    'Now run the search
    strSQL = "Select * FROM Parts WHERE PartNo LIKE '" + searchString +
    "'"
    'now run the query using a data adapter
    Dim PartDataAdapter As New OleDb.OleDbDataAdapter
    PartDataAdapter.SelectCommand = New OleDb.OleDbCommand(strSQL,
    PartDbConnection)
    PartDataAdapter.Fill(PartDataSet1, "Parts")
    Session("PartDataSet") = PartDataSet1
    'this is where the error occurs
    PartDataGrid.DataBind()
    End Sub
    >
    > Thanks for your help.
    > Julia
    >
    >
    Julia B, Sep 30, 2008
    #2
    1. Advertising

  3. Julia B

    Julia B Guest

    Solved it - I had to set the current page index to 0 on the page load.

    Julia

    "Julia B" wrote:

    > Hi all
    >
    > Got a weird problem with pagination on a datagrid in asp.net 1.1.
    >
    > It's populated depending on user selected criteria (it either displays all
    > or 1 record). It works fine in the following circumstances:
    >
    > Datagrid populated with 1 record then re-populated with all
    > Datagrid populated with 1 record then re-populated with another single record
    > Datagrid populated with multiple records and displaying page 1 then
    > repopulated with 1 record
    >
    > It falls over in the following circumstance:
    >
    > Datagrid is populated with multiple records and displaying page 2 or 3 etc
    > then user attempts to repopulate with 1 record.
    >
    > The error message is: Invalid CurrentPageIndex value. It must be >=0 and <
    > the PageCount. I'm guessing it's something to do with the fact that the
    > current page index is more than 0 and that confuses it, so I tried to reset
    > the pagecount to 0 but got a build error that pagecount is a readonly
    > property.
    >
    > Here's the code:
    >
    > Private Sub PartDataGrid_PageIndexChanged(ByVal source As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
    > PartDataGrid.PageIndexChanged
    > ‘This sub refreshes the datagrid if the page index is changed
    > PartDataGrid.CurrentPageIndex = e.NewPageIndex
    > If txtPartSearch.Text = "" Or txtPartSearch.Text = "(Enter part
    > number)" Then
    > showAllRecords()
    > Else
    > searchRecords()
    > End If
    > PartDataGrid.DataBind()
    > End Sub
    >
    > Private Sub PartDataGrid_Page(ByVal sender As Object, ByVal e As
    > DataGridPageChangedEventArgs)
    > PartDataGrid.CurrentPageIndex = e.NewPageIndex
    > PartDataGrid.DataBind()
    > End Sub
    >
    > Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles cmdSearch.Click
    > ‘This is what the user calls when pressing a search button to display 1 record
    > PartDataGrid.CurrentPageIndex = 0
    > End Sub
    >
    > Private Sub cmdAll_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles cmdAll.Click
    > ‘This is what the user calls when pressing a show all records button
    > PartDataGrid.CurrentPageIndex = 0
    > PartDataSet1.Clear()
    > strSQL = "SELECT * FROM PARTS"
    > Dim PartDataAdapter As New OleDb.OleDbDataAdapter
    > PartDataAdapter.SelectCommand = New OleDb.OleDbCommand(strSQL,
    > PartDbConnection)
    > PartDataAdapter.Fill(PartDataSet1, "Parts")
    > Session("PartDataSet") = PartDataSet1
    > PartDataGrid.DataBind()
    > End Sub
    >
    > Private Sub searchRecords()
    > PartDataSet1.Clear()
    > Dim searchString As String = txtPartSearch.Text
    > Now run the search
    > strSQL = "Select * FROM Parts WHERE PartNo LIKE '" + searchString +
    > "'"
    > 'now run the query using a data adapter
    > Dim PartDataAdapter As New OleDb.OleDbDataAdapter
    > PartDataAdapter.SelectCommand = New OleDb.OleDbCommand(strSQL,
    > PartDbConnection)
    > PartDataAdapter.Fill(PartDataSet1, "Parts")
    > Session("PartDataSet") = PartDataSet1
    > 'This is where I get the error
    > PartDataGrid.DataBind()
    > End Sub
    >
    > Thanks for your help.
    > Julia
    >
    >
    Julia B, Oct 2, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joseph D. DeJohn

    DataGrid Pagination

    Joseph D. DeJohn, Jul 4, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,374
    Daniel Bass
    Jul 4, 2003
  2. Sreejumon[MVP]

    Re: datagrid custom pagination

    Sreejumon[MVP], Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    414
    Sreejumon[MVP]
    Jul 8, 2003
  3. Child
    Replies:
    0
    Views:
    327
    Child
    Aug 19, 2003
  4. Chaprasi
    Replies:
    1
    Views:
    6,078
    Karl Seguin [MVP]
    Feb 2, 2006
  5. joe

    DataGrid Pagination, crawler friendly .NET 1.1

    joe, Aug 22, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    108
Loading...

Share This Page