GridView using an ObjectDataSource - total rows with a custom List

Discussion in 'ASP .Net Web Controls' started by Darren Carter, Jan 22, 2006.

  1. Guys,

    Why can't I get the ObjectDataSource concept to work with a GridView? In my
    BLL I create my functions as follows:

    ---------

    public static List<ContactLookup> List(long userId, int maximumRows, int
    startRowIndex, string sortExpression)
    {
    SqlParameter[] prams = { new SqlParameter("@UserID", userId),
    new
    SqlParameter("@SortExpr",sortExpression)
    };
    return GetMultiple("ContactLookupByUserId", prams, maximumRows,
    startRowIndex);
    }

    ---------

    "ContactLookup" is a strongly-typed custom class that I've created. Here is
    the private function "GetMultiple":

    ---------

    private static List<ContactLookup> GetMultiple(string storedProc,
    SqlParameter[] prams, int maximumRows, int startRowIndex)
    {
    List<ContactLookup> coll = null;
    DataTable reader = null;
    try
    {
    using (SqlConnection conn = ConnectionManager.GetConnection())
    {
    using (DataSet ds = SqlHelper.ExecuteDataset(conn,
    CommandType.StoredProcedure, storedProc, prams))
    {
    reader = ds.Tables[0];
    }
    }
    if (reader != null && reader.Rows.Count > 0)
    {
    int currentIndex = startRowIndex;
    int itemsRead = 0;
    int totalRecords = reader.Rows.Count;
    int capacity = (maximumRows == int.MaxValue || maximumRows >
    totalRecords) ? totalRecords : maximumRows;
    coll = new List<ContactLookup>(totalRecords);
    while (itemsRead < maximumRows && currentIndex < totalRecords)
    {
    ContactLookup contactLookup = new ContactLookup();
    contactLookup.ID = Convert.ToInt64(reader.Rows[currentIndex][0]);
    contactLookup.FirstName =
    Convert.ToString(reader.Rows[currentIndex][1]);
    contactLookup.LastName =
    Convert.ToString(reader.Rows[currentIndex][2]);
    coll.Add(contactLookup);
    itemsRead++;
    currentIndex++;
    }
    }
    }
    catch (Exception ex)
    {
    ExceptionManager.Publish(ex);
    }
    finally
    {
    if (reader != null) reader.Dispose();
    }
    return coll;
    }

    ---------

    When I use these functions, my GridView ignores the Capacity property of the
    List<T> and it actually passes -1 for maximumRows. This was all working fine
    in the .NET 2.0 Beta 2, but now that I've moved to the full version of .NET
    2.0, I have this problem. My ObjectDataSource is:

    <asp:ObjectDataSource ID="odsContactLookup" runat="server"
    SelectMethod="List" TypeName="myApp.domain.managers.ContactLookupManager"
    SortParameterName="sortExpression" EnablePaging="True"
    MaximumRowsParameterName="maximumRows"
    StartRowIndexParameterName="startRowIndex">
    <SelectParameters>
    <asp:ControlParameter Name="userId" Type="Int64" ControlID="txtUserID"
    PropertyName="Value" />
    <asp:ControlParameter ControlID="gvContactLookup" DefaultValue=""
    Name="maximumRows" PropertyName="PageSize" Type="Int32" />
    <asp:ControlParameter ControlID="gvContactLookup" DefaultValue=""
    Name="startRowIndex" PropertyName="PageIndex" Type="Int32" />
    <asp:ControlParameter ControlID="gvContactLookup"
    Name="sortExpression" PropertyName="SortExpression" Type="String" />
    </SelectParameters>
    </asp:ObjectDataSource>

    If I have coll = new List<ContactLookup>(capacity ) instead of coll = new
    List<ContactLookup>(totalRecords) in my GetMultiple function (which is what I
    had originally back in Beta 2 when it was working fine) then I get the
    following error on my page:

    If a data source does not return ICollection and cannot return the total row
    count, it cannot be used by the myGridView to implement server-side paging.

    I would prefer to have a stateless environment and don't want to use the
    SelectCountMethod property of the ObjectDataSource.

    What am I missing? Any suggestions?

    Darren
    Darren Carter, Jan 22, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZGF2aWQ=?=

    How to get the total number of rows in a Dataset?

    =?Utf-8?B?ZGF2aWQ=?=, May 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    17,244
    Yunus Emre ALPĂ–ZEN [MCAD.NET]
    May 3, 2005
  2. thomson
    Replies:
    1
    Views:
    788
    Brock Allen
    Jun 11, 2005
  3. Craig Buchanan
    Replies:
    1
    Views:
    2,451
    Reggie
    Oct 2, 2008
  4. VijayRama
    Replies:
    2
    Views:
    2,296
    Gregory A. Beamer
    Oct 12, 2009
  5. yonialhadeff

    Group By Rows and Total Rows

    yonialhadeff, Jan 23, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    839
    Eliyahu Goldin
    Jan 23, 2007
Loading...

Share This Page