Databinding to custom collections

R

Rob Miles

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top