Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Index Out of Range on ListView (with asp:Timer which swich data based on datapager)

Posted on 2008-10-14
2
Medium Priority
?
1,236 Views
Last Modified: 2012-05-05
Greetings, greetings, and thank you.. for reading my problem.

The thing is that i'm getting this error :
"System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

... the SECOND time the data is bound to the listview.. the code will explain the situation for itself..
The first time, i'm getting the datakey the correct way, but the second time.. the error occurs on this line of code in the itemdatabound event :
--> DataKey currentDataKey = this.lvCampain.DataKeys[currentItem.DataItemIndex];

A quick description of the functionality is that there is ONE object per PAGE, and the timer_click event switch pages after 10 seconds..

Any help regarding this would be of great help to me, since this is quite urgent (and i have to work until i get this thing right!)..

So... thanx in advance to whoever has time to assist me. Cheers !

See the attached code and explanation :
ASPX : THE TIMER AND LISTVIEW : 
-------------------------------------------
<asp:Timer ID="timCampain" runat="server" Interval="7000" OnTick="timCampain_Tick" />
                <asp:UpdatePanel runat="server" ID="upnlCampain" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:Panel runat="server" Width="100%" ID="pnlCampain">
                        
                            <asp:ListView runat="server" ID="lvCampain" DataKeyNames="Id" DataSourceID="odsCampain"
                                OnItemCommand="lvCampain_OnItemCommand" OnItemDataBound="lvCampain_ItemDataBound">
                                <LayoutTemplate>
                                    <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                                </LayoutTemplate>
                                <ItemTemplate>
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td valign="top" width="70%">
                                                <table width="100%" border="0" cellspacing="2" cellpadding="2">
                                                    <tr>
                                                        <td>
                                                            <div style="float: left; vertical-align: top;">
                                                                <asp:Label runat="server" ID="lblUgle" /> /// ETC ETC ETC......
 
ASPX : OBJECTDATASOURCE AND DATAPAGER:
------------------------------------------------
</asp:ListView>
                            <div style="float: left; margin-left: 5px">
                                <asp:Panel runat="server" ID="pnlPager" CssClass="listviewpager" Enabled="True" Visible="True">
                                    <asp:DataPager ID="dpCampain" runat="server" PagedControlID="lvCampain" PageSize="1">
                                        <Fields>
                                            <asp:NumericPagerField />
                                        </Fields>
                                    </asp:DataPager>
                                </asp:Panel>
                            </div>
                            <asp:ObjectDataSource ID="odsCampain" runat="server" TypeName="Dao.initiativeDao, App_Code"
                                SelectMethod="activeCampain" EnablePaging="True" SelectCountMethod="activeCampainCount"
                                StartRowIndexParameterName="startRow" MaximumRowsParameterName="maxRow">
                                <SelectParameters>
                                    <asp:SessionParameter DefaultValue="0" SessionField="companyid" Name="companyid"
                                        Type="Int32" />
                                </SelectParameters>
                            </asp:ObjectDataSource>
 
ASPX.CS : THE TIMER_CLICK EVENT : 
 
protected void timCampain_Tick(object sender, EventArgs e)
    {
        int currPage = dpCampain.StartRowIndex;
        int lastPage = dpCampain.TotalRowCount;
 
        if (currPage < lastPage - 1)
        {
            dpCampain.SetPageProperties(currPage + 1, 1, true);
        }
        else
	    {
            dpCampain.SetPageProperties(0, 1, true);
	    }
 
        upnlCampain.Update();
    }
 
ASPX.CS : ITEMDATABOUND EVENT : 
 
protected void lvCampain_ItemDataBound(object sender, ListViewItemEventArgs e)
      {
          if (e.Item.ItemType == ListViewItemType.DataItem)
           {
              ListViewDataItem currentItem = (ListViewDataItem)e.Item;
              
              DataKey currentDataKey = this.lvCampain.DataKeys[currentItem.DataItemIndex];
 
              Label lblUgle = (Label)currentItem.FindControl("lblUgle");
 
              lblUgle.Text = Convert.ToString((int)currentDataKey["Id"]);
 
              //If ( NOT e.Item.ItemIndex > departmentsGrid.DataKeys.Count ) Then
              //  departmentId = departmentsGrid.DataKeys(e.Item.ItemIndex)
              //End If
 
           }
       }

Open in new window

0
Comment
Question by:Nygter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Author Comment

by:Nygter
ID: 22716480
Oh.. and by the way.. i need to get the primary-key (Id) of the object for further use in a query in the itemdatabound event.
0
 

Accepted Solution

by:
Nygter earned 0 total points
ID: 22716551
Found the solution myself mostly due to luck :

protected void lvCampain_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem currentItem = (ListViewDataItem)e.Item;
            int currentId = ((Induct.Model.Campain)currentItem.DataItem).Id;

            Label lblUgle = (Label)currentItem.FindControl("lblUgle");
            lblUgle.Text = currentId.ToString();
        }
    }
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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