Listview Datapager - Index was out of range. Must be non-negative and less than the size of the collection <p>I am using Listview to display products list from database. OnPage Load products gets load properly but when I try to switch page using datapager pagination it gives this error</p> <p>Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayList.get_Item(Int32 index) at System.Web.UI.WebControls.DataKeyArray.get_Item(Int32 index) at shop.products_ItemDataBound(Object sender, ListViewItemEventArgs e) </p> <p>Thing is if I use ItemDataBound then only I get this error. Else paginations works fine.</p> <pre><code>&lt;asp:ListView ID="products" runat="server" DataKeyNames="ID" OnPagePropertiesChanging="OnPagePropertiesChanging"&gt; &lt;ItemTemplate&gt; content &lt;/ItemTemplate&gt; &lt;LayoutTemplate&gt; &lt;div id="itemPlaceholderContainer" runat="server" style=""&gt; &lt;div runat="server" id="itemPlaceholder" /&gt; &lt;/div&gt; &lt;div class="datapager"&gt; &lt;asp:DataPager ID="DataPager1" ClientIDMode="Static" runat="server" PageSize="12" PagedControlID="products" ViewStateMode="Enabled"&gt; &lt;Fields&gt; &lt;asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="False" ShowNextPageButton="false" ButtonCssClass="nextPre" /&gt; &lt;asp:NumericPagerField ButtonType="Link" ButtonCount="10" /&gt; &lt;asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" ButtonCssClass="nextPre" /&gt; &lt;/Fields&gt; &lt;/asp:DataPager&gt; &lt;div class="clear"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/LayoutTemplate&gt; &lt;EmptyDataTemplate&gt; &lt;strong&gt;No Items Found....&lt;/strong&gt; &lt;/EmptyDataTemplate&gt; &lt;/asp:ListView&gt; </code></pre> <p>Item DataBound</p> <pre><code>private void products_ItemDataBound(object sender, ListViewItemEventArgs e) { try { if (e.Item.ItemType == ListViewItemType.DataItem) { ListViewDataItem itm = (ListViewDataItem)e.Item; string productID = products.DataKeys(itm.DataItemIndex)("ID"); query = "SELECT stock_status FROM products WHERE ID = '" + productID + "'"; DataTable dt = this.GetData(query); if (dt.Rows.Count &gt; 0) { ((Label)e.Item.FindControl("checkReadyStock")).Text = dt.Rows(0)("stock_status").ToString; if (((Label)e.Item.FindControl("checkReadyStock")).Text == "Ready Stock") { ((Image)e.Item.FindControl("readyStock")).Visible = true; } } } } catch (Exception ex) { Response.Write(ex); } } </code></pre> <p>DataPager</p> <pre><code>protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) { (products.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false); products.DataSource = ViewState("Data"); products.DataBind(); } </code></pre>