469,133 Members | 1,458 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

Pagination problem with asp.net 1.1 datagrid

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.DataGridPageChangedEvent Args) 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
Sep 30 '08 #1
2 1451
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.DataGridPageChangedEvent Args) 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

Sep 30 '08 #2
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.DataGridPageChangedEvent Args) 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

Oct 2 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mukesh Kumar | last post: by
1 post views Thread by Kruq | last post: by
3 posts views Thread by Joseph D. DeJohn | last post: by
reply views Thread by Don Quijote de Nicaragua | last post: by
16 posts views Thread by gnawz | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.