Sorting children lists in parent object???

Sorting children lists in parent object???

Old forum URL: forums.lhotka.net/forums/t/774.aspx


david.wendelken posted on Monday, July 31, 2006

I'm having trouble with sorting child collections on my web pages.

I have the following classes:

I used NameInfoList as the data source for a GridView and implemented the sorting feature on it.  Here's the C# code snippet where I return the sorted list:

private SortedBindingList<The.Library.NameInfo> GetNameInfoList()
{
  object obj = Session[GetCurrentObjectCacheName()];
  if (   obj == null
      || !(obj is SortedBindingList<The.Library.NameInfo>)
     )
  {
    // Set parameter values.
    string    parentName = tbxSearch.Text + "%";

    NameInfoList list = NameInfoList.GetList(parentName); 
    obj = new SortedBindingList<NameInfo>
                  (NameInfoList.GetList(parentName));            
    HttpContext.Current.Session[GetCurrentObjectCacheName()] = obj;

 }
 return ((SortedBindingList<The.Library.NameInfo>)obj);
}

Works like a charm!

Here's my problem.  I have a "master object", one of whose properties is a NameInfoList.  That NameInfoList is in a GridView on the same page with the master object.  I want to enable sorting on the NameInfoList grid on this page also.  To avoid lots of round-trips, the master object is responsible for populating all the data that's inside it.  So, my cache is actually the master object , not a NameInfoList object.

Here's the error I'm getting:

Unable to cast object of type 'The.Library.NameInfoList' to type 'Csla.SortedBindingList`1[The.Library.NameInfo]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'The.Library.NameInfoList' to type 'Csla.SortedBindingList`1[The.Library.NameInfo]'.

Source Error:

Line 93:         Main obj = GetMain();
<-- Note: My NameInfoList in Main is called Names -->
Line 94:         Object names = obj.Names; 

Line 95:         return ((SortedBindingList<The.Library.NameInfo>)names);
Line 96:     }
Line 97:     #endregion
 

Source File: c:\Inetpub\wwwroot\TheWeb\DataReports\Detail.aspx.cs    Line: 95

Stack Trace:


[InvalidCastException: Unable to cast object of type 'The.Library.NameInfoList' to type 'Csla.SortedBindingList`1[The.Library.NameInfo]'.]
   DataReports_Detail.GetNames() in c:\Inetpub\wwwroot\TheWeb\DataReports\Detail.aspx.cs:95
   DataReports_Detail.NameDS_SelectObject(Object sender, SelectObjectArgs e) in c:\Inetpub\wwwroot\TheWeb\DataReports\Detail.aspx.cs:82
   Csla.Web.CslaDataSource.OnSelectObject(SelectObjectArgs e) in C:\MyProj\Csla\Web\CslaDataSource.cs:123
   Csla.Web.CslaDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) in C:\MyProj\Csla\Web\CslaDataSourceView.cs:60
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +84
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
   System.Web.UI.WebControls.GridView.DataBind() +23
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +92
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +100
   System.Web.UI.Control.EnsureChildControls() +134
   System.Web.UI.Control.PreRenderRecursiveInternal() +109
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4437

Here's the code returning the sorted binding list... 

private SortedBindingList<The.Library.NameInfo> GetNames()
{
  UICMain obj = GetUICMain();
  Object names = obj.Names; // .Names is a NameInfoList class.
  return ((SortedBindingList<UIC.Library.UICNameInfo>)names);
}

Frankly, I'm confused. :)

david.wendelken replied on Tuesday, August 01, 2006

bump.

david.wendelken replied on Thursday, August 03, 2006

Sorry foks, but I'm still stumped on this one.  Can anyone help out?

Brian Criswell replied on Friday, August 04, 2006

Well, ASP.NET is not my forte, but can you post the code to Main.Names?  It looks like it is returning a NameInfoList instead of a SortedBindingList.

ajj3085 replied on Friday, August 04, 2006

I think you need to change line 95 in the code posted to this:

return new SortedBindingList<The.Library.NameInfo>( names );

The problem is that will return a new sorted list; so the next page refresh would 'lose' the current sort... so you should cache the SortedBindingList and then read from the cache.

HTH
Andy

Copyright (c) Marimer LLC