Pagination problem with asp.net 1.1 datagrid

  • Thread starter Thread starter Julia B
  • Start date Start date
J

Julia B

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
 
My apologies, I simplified the code from original and made a mistake. Please
see updated version below.

Julia B said:
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
 
Back
Top