Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   "gridview Object does not match target type" error during binding collection of different type object (http://www.velocityreviews.com/forums/t121226-gridview-object-does-not-match-target-type-error-during-binding-collection-of-different-type-object.html)

gui.besse@gmail.com 03-09-2006 10:28 AM

"gridview Object does not match target type" error during binding collection of different type object
 
It seems that we can't bind a collection of instance of different type.

Let's have an example:

// My POCO
public interface ITest
{ string Name { get;set;} }

public class A : ITest
{
private string _name;
public string Name
{
get{ return _name;}
set{ _name = value;}
}

public class B : ITest
{
private string _name;
public string Name
{
get{ return _name;}
set{ _name = value;}
}

// My asp.net page
// 1 - populate my list
List<ITest> myList = new List<ITest>();
A a = new A();
a.Name = "my name is A";
myList.Add(a)

B b = new B();
b.Name = "my name is B";
myList.Add(b);

// the binding with gridview
myGridView.DataSource = myList:
myGridView.DataBind();

generate an exception on the last line:

Object does not match target type.
Exception Details: System.Reflection.TargetException: Object does not
match target type.
[TargetInvocationException: Property accessor 'Name' on object 'B'
threw the following exception:'Object does not match target type.']

Any idea?


djnic 07-07-2006 08:53 AM

Same problem with DataGrid
 
Hi,

I have a similary problem with a DataGrid bounded to an ArrayList containing several object classes. All these classes are sub-classes of a parent class.

e.g.:

public class A
{
public virtual string Name
{
get {return string.Empty;}
}
}

public class AA : A
{
private string name;
public override string Name
{
get {return name;}
}
}

public class AB : A
{
private string fullName;
public override string Name
{
get {return fullName;}
}
}


If my ArrayList contains a class AA object and a class AB object, when the AA object if bounded, the Name property is used to bind a column of the DataGrid : everything works fine. When the AB object is then bounded, I catch a TargetInvocationException with the message:
Property accessor 'Name' on object 'AB' threw the following exception:'Object does not match target type.

Here is the full stack trace:

[TargetException: Object does not match target type.]
System.Reflection.RuntimeMethodInfo.InternalInvoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess) +0
System.Reflection.RuntimeMethodInfo.InternalInvoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean verifyAccess) +422
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +23
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +14
System.ComponentModel.ReflectPropertyDescriptor.Ge tValue(Object component)

[TargetInvocationException: Property accessor 'Name' on object 'AB' threw the following exception:'Object does not match target type.']
System.ComponentModel.ReflectPropertyDescriptor.Ge tValue(Object component)
System.Web.UI.WebControls.ButtonColumn.OnDataBindC olumn(Object sender, EventArgs e)
System.Web.UI.Control.OnDataBinding(EventArgs e)
System.Web.UI.Control.DataBind()
System.Web.UI.Control.DataBind()
System.Web.UI.Control.DataBind()
System.Web.UI.WebControls.DataGrid.CreateItem(Int3 2 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource)
System.Web.UI.WebControls.DataGrid.CreateControlHi erarchy(Boolean useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBindi ng(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
System.Web.UI.Control.DataBind()
TNS.Framework.Production.Web.Controls.SupportFinde rControl.DataBind() in ...\myPage.cs:107
TNS.Framework.Production.Web.Controls.SupportFinde rControl.btnSearch_Click(Object sender, EventArgs e) in ...\myPage.cs:258
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain()


I Think that the DataGrid instanciate a ReflectPropertyDescriptor of the first object, with its concrete class (AA), and uses this instance while binding the second object, so the ReflectPropertyDescriptor does not match the correct class of the second object (AB).

Did you manage to resolve your problem ? If yes, how ?

Thanks for reply.

Sean Foy 08-06-2006 03:12 PM

Databinding against heterogeneous collections
 
I've had this problem, and found two solutions: either implement ITypedList, or make your collection homogeneous. The second option is not so distasteful as it may sound, because you can automate the process of generating a suitable type to represent commonalities among the objects you are databinding.

You can find more information including an implementation of this second idea on my DefensiveDatasource page.


All times are GMT. The time now is 09:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.