title: Error: "Capacity was less than the current size" with ASP.NET DataPager control
body: <p>I've got an ASP.NET ListView with a DataPager as per this code: </p>
  
  <pre><code>        &lt;asp:ListView ID="TicketsPerPersonListView" runat="server" DataSourceID="ObjectDataSource1"&gt;
          &lt;EmptyDataTemplate&gt;
              -- No Records Found --
          &lt;/EmptyDataTemplate&gt;
          &lt;ItemTemplate&gt;
              &lt;tr style="display: table-row;"&gt;
                  &lt;td&gt;
                      &lt;a href="viewticket.aspx?id=&lt;%#Eval("ticket_id")%&gt;"&gt;#&lt;%# Eval("TicketID")%&gt;-
                          &lt;%# Eval("ShortDesc")%&gt;&lt;/a&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;%# Eval("StatusName")%&gt;
                  &lt;/td&gt;
                  &lt;td style="text-align: right; padding-right: 60px;"&gt;
                      &lt;%# Eval("StatusDescription")%&gt;
                  &lt;/td&gt;
                  &lt;td style="text-align: right; padding-right: 60px;"&gt;
                      &lt;%# Eval("TimeLastAction")%&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/ItemTemplate&gt;
          &lt;LayoutTemplate&gt;
              &lt;table&gt;
                  &lt;thead&gt;
                      &lt;tr&gt;
                          &lt;th&gt;
                              Title
                          &lt;/th&gt;
                          &lt;th style="text-align: right; padding-right: 60px;"&gt;
                              Current Status
                          &lt;/th&gt;
                          &lt;th style="text-align: right; padding-right: 60px;"&gt;
                              Latest Action
                          &lt;/th&gt;
                          &lt;th style="text-align: right; padding-right: 60px;"&gt;
                              Last Viewed
                          &lt;/th&gt;
                      &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tfoot&gt;
                      &lt;tr&gt;
                          &lt;td colspan="5"&gt;
                              &lt;div class="pagination"&gt;
                                  &lt;asp:DataPager ID="TicketsPerPersonDataPager" runat="server" PagedControlID="TicketsPerPersonListView"
                                      PageSize="10"&gt;
                                      &lt;Fields&gt;
                                          &lt;asp:NextPreviousPagerField FirstPageText="&amp;lt;&amp;lt;" ShowFirstPageButton="True" ShowNextPageButton="False"
                                              ShowPreviousPageButton="False" /&gt;
                                          &lt;asp:NumericPagerField CurrentPageLabelCssClass="graybutton pagelink active" NumericButtonCssClass="graybutton pagelink" /&gt;
                                          &lt;asp:NextPreviousPagerField LastPageText="&amp;gt;&amp;gt;" ShowLastPageButton="True" ShowNextPageButton="False"
                                              ShowPreviousPageButton="False" /&gt;
                                      &lt;/Fields&gt;
                                  &lt;/asp:DataPager&gt;
                              &lt;/div&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/tfoot&gt;
                  &lt;tbody&gt;
                      &lt;asp:PlaceHolder runat="server" ID="itemPlaceholder"&gt;&lt;/asp:PlaceHolder&gt;
                      &lt;tr style="display: table-row;"&gt;
                      &lt;/tr&gt;
                  &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/LayoutTemplate&gt;
      &lt;/asp:ListView&gt;
      &lt;asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnablePaging="True" SelectMethod="FindByUserID"
          TypeName="Xyratex.XCS.Services.TicketService" StartRowIndexParameterName="startRowIndex"
          MaximumRowsParameterName="maximumRows" SelectCountMethod="CountByUserID"&gt;
          &lt;SelectParameters&gt;
              &lt;asp:Parameter Name="userID" DbType="Int64" DefaultValue="79" /&gt;
              &lt;asp:ControlParameter Name="custID" ControlID="DDCaller" ConvertEmptyStringToNull="True"
                  DefaultValue="" /&gt;
              &lt;asp:ControlParameter Name="company" ControlID="DDCompany" ConvertEmptyStringToNull="True"
                  DefaultValue="" /&gt;
              &lt;asp:ControlParameter Name="owner" ControlID="DDOwner" ConvertEmptyStringToNull="True"
                  DefaultValue="" /&gt;
              &lt;asp:ControlParameter Name="action" ControlID="DDAction" ConvertEmptyStringToNull="True"
                  DefaultValue="" /&gt;
              &lt;asp:ControlParameter Name="status" ControlID="DDCurrentStatus" ConvertEmptyStringToNull="True"
                  DefaultValue="" /&gt;
          &lt;/SelectParameters&gt;
      &lt;/asp:ObjectDataSource&gt;
  </code></pre>
  
  <p>The VB.NET code that provides the ObjectDataSource with data is thus:</p>
  
  <pre><code>    ''' &lt;summary&gt;
  ''' Finds all the &lt;see cref="Xyratex.XCS.Model.Tickets.Ticket" /&gt; objects belonging to the specified user ID
  ''' &lt;/summary&gt;
  ''' &lt;param name="userID"&gt;The id of the user to find tickets for&lt;/param&gt;
  ''' &lt;param name="startRowIndex"&gt;The starting index of the portion of the recordset&lt;/param&gt;
  ''' &lt;param name="maximumRows"&gt;The maximum amount of rows to return&lt;/param&gt;
  ''' &lt;param name="custID"&gt;The id of the customer to find tickets for&lt;/param&gt;
  ''' &lt;param name="company"&gt;The company name to find tickets for&lt;/param&gt;
  ''' &lt;param name="owner"&gt;The owner to find tickets for&lt;/param&gt;
  ''' &lt;param name="action"&gt;The action to find tickets for&lt;/param&gt;
  ''' &lt;param name="status"&gt;The status of tickets to find.&lt;/param&gt;
  ''' &lt;returns&gt;A &lt;see cref="System.Collections.Generic.IList(Of Xyratex.XCS.Model.Tickets.Ticket)" /&gt; object&lt;/returns&gt;
  ''' &lt;remarks&gt;XY01\rpenfold 30 January 2012&lt;/remarks&gt;
  Public Function FindByUserID(ByVal userID As Long,
                               ByVal startRowIndex As Long,
                               ByVal maximumRows As Long,
                               ByVal custID As Long?,
                               ByVal company As String,
                               ByVal owner As Integer?,
                               ByVal action As Char?,
                               ByVal status As Long?) As List(Of LightWeightTicket) Implements ITicketRepository.FindByUserID
      'Start a new session and run the query
      Using session As NHibernate.ISession = SessionFactory.GetNewSession()
          Dim query As NHibernate.IQuery = session.GetNamedQuery("Select_AllTickets_ByUserID_RyanTest")
          query.SetInt64("user_id", userID)
          Select Case custID.HasValue
              Case True
                  query.SetInt64("cust_id", custID.Value)
              Case False
                  query.SetString("cust_id", Nothing) 'SetString makes it NULL
          End Select
          Select Case String.IsNullOrWhiteSpace(company)
              Case True
                  query.SetString("company", Nothing)
              Case False
                  query.SetString("company", company) 'SetString makes it NULL
          End Select
          Select Case owner.HasValue
              Case True
                  query.SetInt32("owner", owner.Value)
              Case False
                  query.SetString("owner", Nothing) 'SetString makes it NULL
          End Select
          Select Case action.HasValue
              Case True
                  query.SetCharacter("action", action.Value)
              Case False
                  query.SetString("action", Nothing) 'SetString makes it NULL
          End Select
          Select Case status.HasValue
              Case True
                  query.SetInt64("status", status.Value)
              Case False
                  query.SetString("status", Nothing) 'SetString makes it NULL
          End Select
          query.SetInt64("startRowIndex", startRowIndex)
          query.SetInt64("maximumRows", maximumRows)
          Return New List(Of LightWeightTicket)(query.List(Of LightWeightTicket)())
      End Using
  End Function
  </code></pre>
  
  <p>When I load the page, I receive the error "Capacity was less than the current size". The stack trace is per thus:</p>
  
  <pre><code>[ArgumentOutOfRangeException: capacity was less than the current size.
  </code></pre>
  
  <p>Parameter name: value]
      System.Collections.ArrayList.set_Capacity(Int32 value) +9360651
      System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +712
      System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data) +35
      System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
      System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
      System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
      System.Web.UI.WebControls.ListView.PerformSelect() +113
      System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
      System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
      System.Web.UI.WebControls.ListView.CreateChildControls() +55
      System.Web.UI.Control.EnsureChildControls() +102
      System.Web.UI.Control.PreRenderRecursiveInternal() +42
      System.Web.UI.Control.PreRenderRecursiveInternal() +175
      System.Web.UI.Control.PreRenderRecursiveInternal() +175
      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496</p>
  
  <p>Does anyone know what to do about this?</p>
  
  <p>Thanks in advance,</p>
  
  <p>Ryan</p>