D
Darren Carter
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
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