Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
I'm using a datalist...but when I laod the control I get this error...But not everytime...
How to resolve this??
The error occurs at
Line
dlstPosts.DataBind();

objPage.FirstIndexInPage=-10
When I m debugging...Is this the issue?


DataSet dstAllPosts = new DataSet();
       .
       .//Filling data
       .
        PagedDataSource objPage = new PagedDataSource();
        objPage.AllowPaging = true;
        objPage.DataSource = dstAllPosts.Tables[0].DefaultView;
        objPage.PageSize = 10;
        objPage.CurrentPageIndex = currentPage;

        if (dstAllPosts.Tables[0].Rows.Count > 10)
        {
            lbtNext.Enabled = !objPage.IsLastPage;
            lbtPrev.Enabled = !objPage.IsFirstPage;
        }
        else
        {
            lbtNext.Enabled = false;
            lbtPrev.Enabled = false;
        }
       dlstPosts.DataSource = objPage;
       dlstPosts.DataBind();





VB
[IndexOutOfRangeException: Index -10 is either negative or above rows count.]
   System.Data.DataView.GetRow(Int32 index) +77
   System.Data.DataView.System.Collections.IList.get_Item(Int32 recordIndex) +10
   System.Web.UI.WebControls.EnumeratorOnIList.get_Current() +23
   System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +520
   System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +57
   System.Web.UI.WebControls.BaseDataList.DataBind() +62

.
.
.
VB
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +43
   System.Web.UI.Control.OnLoad(EventArgs e) +80
   System.Web.UI.Control.LoadRecursive() +49
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3746


Thanks in advance...
Posted
Updated 28-Feb-11 23:44pm
v4

IndexOutOfRangeException
IndexOutOfRangeException: Index -10 is either negative or above rows count.

Error type and the contents are self-explanatory in themselves. You are trying to use/access certain index which does not exists for a given collection object.

A simple use of DEBUGGER should easily help you out. Just look around the code line where you get this error and correct the logic that is trying to access the index which does not exist.

Details on the same:
IndexOutOfRangeException Class[^]

This surely would help: Troubleshooting Exceptions: System.IndexOutOfRangeException[^]


BTW, just by looking from naked eyes, start from here:
C#
objPage.DataSource = dstAllPosts.Tables[0].DefaultView;
objPage.PageSize = 10;
objPage.CurrentPageIndex = currentPage;

Error says -10 kinda thing. Looks like nothing was returned and yet you try to set it to some pagination. Check for number of records returned before setting up the pagination and counts.
 
Share this answer
 
Comments
aswathy.s.88 1-Mar-11 5:44am    
The error occurs at
Line
dlstPosts.DataBind();

objPage.FirstIndexInPage=-10
When I m debugging...Is this the issue?
Sandeep Mewara 1-Mar-11 5:52am    
objPage.FirstIndexInPage=-10
Yes, this is wrong. Resolve the logic.
aswathy.s.88 1-Mar-11 5:54am    
But it is not possible to manually set objPage.FirstIndexInPage property
Sandeep Mewara 1-Mar-11 5:56am    
Dude, as I said, handle pagination if you have more than 10 records.

Start step wise:
1. Remove pagination thing. Error should go away.
2. Put pagination logic and control but only if the record count is greater than 10 (page size)
aswathy.s.88 1-Mar-11 5:58am    
Tanks...Let me try.... :)
Hi,

you should check before index assign like

C#
if(dstAllPosts.Tables.Count > 0)objPage.DataSource = dstAllPosts.Tables[0].DefaultView;

if(currentPage >=0) objPage.CurrentPageIndex = currentPage;


Thanks,
Imdadhusen
 
Share this answer
 
try with dstAllPosts.Tables[0].Rows.Count >= 10 :


C#
if (dstAllPosts.Tables[0].Rows.Count >= 10)
        {
            lbtNext.Enabled = !objPage.IsLastPage;
            lbtPrev.Enabled = !objPage.IsFirstPage;
        }
        else
        {
            lbtNext.Enabled = false;
            lbtPrev.Enabled = false;
        }
 
Share this answer
 
Comments
aswathy.s.88 1-Mar-11 5:40am    
But again I face the same error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900