Solved

Invalid CurrentPageIndex value

Posted on 2009-07-08
17
473 Views
Last Modified: 2012-06-27
It was working fine and then boom all the sudden started to get this message.  I have not enabled anthing in the datagrid to be deleted or updated yet, the error starts from the page load. When i click on a page number it throw the error.
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
I have looked at several solution but non seem to work, don't know why.  the follwoing is my code for my pageindexchanged.  I added the if then else statment to try to resolve issue but I still get the error.
Private Sub myDataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles myDataGrid.PageIndexChanged
        'Enable datagrid paging
        If myDataGrid.CurrentPageIndex >= myDataGrid.PageCount Then
            myDataGrid.CurrentPageIndex = myDataGrid.PageCount - 1
        End If
        myDataGrid.CurrentPageIndex = e.NewPageIndex
        myDataGrid.DataSource = myDataTable
        myDataGrid.DataBind()
 
    End Sub

Open in new window

0
Comment
Question by:kdeutsch
  • 7
  • 7
  • 2
  • +1
17 Comments
 
LVL 2

Expert Comment

by:lppperes
ID: 24808440
Try using
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = myDataTable
        GridView1.DataBind();
0
 
LVL 15

Expert Comment

by:jinal
ID: 24810022
Please verify that myDataTable contains records that you want to display.

For example  myDataTable should not be null or it should not changed. Like previously it has 100 records and now it has only 20 records . So if you give new page index and that out of scope so that error occur.

0
 

Author Comment

by:kdeutsch
ID: 24813150
lppperes,
That is what I originally started with but since I am still using asp.net 1.1 I need to use as such. It wa working then all the sudden stopped working, they do not have an ability to delete any of the files.  Paging does not work from the start.
 myDataGrid.CurrentPageIndex = e.NewPageIndex
        myDataGrid.DataSource = myDataTable
        myDataGrid.DataBind()

jinal,
Not sure what you are talking about.  It show the records, same amount as before but paging just stopped working.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 22

Expert Comment

by:prairiedog
ID: 24815368
Make sure you don't rebind the DataGrid upon postback, which means you have a "If Not IsPostBack Then" block in Page_Load.
0
 

Author Comment

by:kdeutsch
ID: 24815859
HI,
this is the only code I have in postback statement, so are you saying I need to put the rebind in my postback.  Right now I am just filling a dropDown list.  Attached is all my code behind.
Help.txt
0
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 200 total points
ID: 24816921
I think I know where the problem is.

At line 7 of the code snippet in your question, "myDataTable" seems to be empty. Where do you populate "myDataTable"?
0
 
LVL 15

Accepted Solution

by:
jinal earned 300 total points
ID: 24817494
Try the following .

You need to store DataTable somewhere then retrieve back to for future use.

Here when your drop down selection index changed than you bind GridView but you did not store any data.

So when you change page index of grid view and myDataTAble is null . so problem. Try following .

Private Sub ddlUic_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUic.SelectedIndexChanged
        Dim Unit As String = ddlUic.SelectedValue
 
        sql = "select intPositionId, strPara as Para, strLine as Line, intPositionNum as [Position #], strGrade as Grade, Substring(strMos,1,4) as MOS, strPosntitle as Title, " _
            & "strAuthBr as Branch, strFillType as Fill, bitAccepted as Accept from tblUnitPosition where intUnitMobId = '" & Unit & "'"
 
        myDataTable = New DataTable
        myDataTable = getData(sql)
        ViewState("DataToBind")=  myDataTable
        mydatagrid.DataSource = myDataTable
        mydatagrid.DataBind()
 
    End Sub
 
 
    Private Sub myDataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles myDataGrid.PageIndexChanged
        
        myDataTable = (DataTable)ViewState("DataToBind")
        myDataGrid.CurrentPageIndex = e.NewPageIndex
        myDataGrid.DataSource = myDataTable
        myDataGrid.DataBind()
 
    End Sub

Open in new window

0
 

Author Comment

by:kdeutsch
ID: 24817703
Hi,
OK I changed the bind as recommended with this statement added
 ViewState(sql) = myDataTable

But I am getting blue underlines with this statement in the pageIndexChanged statement
 MyDataTable = (DataTable)ViewState("Sql")  

Get Seperate blue underlines under Datable and ViewState
DataTable is a type and cannot be used as expression
End of satement expected

If I put in the "" in Daatable as such ("DataTable")
then i get on error
End of Statement expected
   
0
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 200 total points
ID: 24817795
>>>MyDataTable = (DataTable)ViewState("Sql")
It's C# style, in VB.NET it should be:

myDataTable = CType(ViewState("Sql"), DataTable)
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24817829
Also keep in mind that ViewState is case sensitive, which means ViewState("sql") is not the same as ViewState("Sql")
0
 

Author Comment

by:kdeutsch
ID: 24818020
prairiedog,
Ok did not get the error but my datagrid disappeared.  Do I need to put this in my Postback load event
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24818121
Post your current code please. It seems to me that myDataTable is empty.
0
 

Author Comment

by:kdeutsch
ID: 24818179
Hi prairiedog,

I figured it out by looking at you past posts and making sure everything was same, I missed the "" around my dataFill.   One question though, now I need to start saving data in the grid to a DB with other information and I will start taking rows out of the datagrid.  Will this code still work??
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24818210
>>now I need to start saving data in the grid to a DB with other information and I will start taking rows out of the datagrid.

Can you elaborate a bit?
0
 

Author Comment

by:kdeutsch
ID: 24822395
HI,
I was babbling on, want the intent is that I search for a name in a db and save it to a specific row of the datagrid, when i do this I need to reload the datagrid.  Will this have an effect on the page index event.
0
 

Author Closing Comment

by:kdeutsch
ID: 31601317
Thanks for everyones help
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24823608
It depends.

If you can ask a related question, then it will easier for us to understand your question better.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
imap read mail 1 30
ASP.net File format issue in producing Excel file 3 40
Send SMS from VB via Sierra Wireless Modem 2 22
C# Gridview 1 33
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question