Databinding to custom collections

Discussion in 'ASP .Net Datagrid Control' started by Rob Miles, Nov 12, 2004.

  1. Rob Miles

    Rob Miles Guest

    Hi,

    I am developing a web application in ASP.NET (Visual Studio 2003, .NET
    Framework v1.1.4322.573). I have a problem with databinding to a collection
    of custom-defined classes.
    I am trying to bind a web forms datagrid to a collection of custom classes.
    The collection contains objects of an abstract type called BranchQuery. At
    runtime the objects in the collection will either be of type ASRBranchQuery
    or ManualBranchQuery, both of which inherit from BranchQuery. This works
    fine as long as the collection contains all ASRBranchQuery objects or all
    ManualBranchQuery objects. However if there is a mixture of the two runtime
    types then I get the exception "System.Reflection.TargetInvocationException"
    (full details at the bottom of the mail).
    I cannot see why this should not work, as BranchQuery is a generalisation of
    ASRBranchQuery and ManualBranchQuery and both of the implementation classes
    have exactly the same method and property signatures.
    I have managed to work around this so far by removing the inheritance from
    my classes and replacing it with conditional statements, but this does not
    strike me as good design and I would like to know whether the problem is
    caused by an error on my part or a bug in the datagrid databinding?

    Thanks in advance,

    Rob Miles


    1) Exception Information
    *********************************************
    Exception Type: System.Reflection.TargetInvocationException
    Message: Property accessor 'EffectiveDate' on object
    'IA_Online.ManualBranchQuery' threw the following exception:'Object does not
    match target type.'
    TargetSite: System.Object GetValue(System.Object)
    HelpLink: NULL
    Source: System

    StackTrace Information
    *********************************************
    at System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object
    component)
    at System.Web.UI.WebControls.BoundColumn.OnDataBindColumn(Object sender,
    EventArgs e)
    at System.Web.UI.Control.OnDataBinding(EventArgs e)
    at System.Web.UI.Control.DataBind()
    at System.Web.UI.Control.DataBind()
    at System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32
    dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem,
    DataGridColumn[] columns, TableRowCollection rows, PagedDataSource
    pagedDataSource)
    at System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean
    useDataSource)
    at System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
    at System.Web.UI.WebControls.BaseDataList.DataBind()
    at IA_Online.BranchQuerySummary.DisplayQueries() in
    C:\CBP\CBP_Online\IA_Online\BranchQuerySummary.aspx.vb:line 289
    at IA_Online.BranchQuerySummary.grdQueries_Load(Object sender, EventArgs
    e) in C:\CBP\CBP_Online\IA_Online\BranchQuerySummary.aspx.vb:line 113

    2) Exception Information
    *********************************************
    Exception Type: System.Reflection.TargetException
    Message: Object does not match target type.
    TargetSite: System.Object InternalInvoke(System.Object,
    System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[],
    System.Globalization.CultureInfo, Boolean, System.Reflection.Assembly,
    Boolean)
    HelpLink: NULL
    Source: mscorlib

    StackTrace Information
    *********************************************
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
    BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
    culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
    BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
    culture, Boolean verifyAccess)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
    invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
    at System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object
    component)
     
    Rob Miles, Nov 12, 2004
    #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?SW1hciBTcGFhbmphYXJz?=

    Databinding to custom properties of a custom class in ASP.NET 2

    =?Utf-8?B?SW1hciBTcGFhbmphYXJz?=, Apr 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    607
    =?Utf-8?B?SW1hciBTcGFhbmphYXJz?=
    Apr 20, 2005
  2. Doug Poland
    Replies:
    9
    Views:
    763
    VisionSet
    Sep 27, 2003
  3. mutex
    Replies:
    0
    Views:
    230
    mutex
    Jul 27, 2003
  4. Earl Teigrob
    Replies:
    1
    Views:
    444
    Shravan
    Mar 1, 2004
  5. Miguel Navarro

    Possible Bug in Lisbox.Databinding for collections

    Miguel Navarro, Dec 5, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    113
    Lostinet.Web Support
    Dec 8, 2003
Loading...

Share This Page