Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > "gridview Object does not match target type" error during binding collection of different type object

Reply
Thread Tools

"gridview Object does not match target type" error during binding collection of different type object

 
 
gui.besse@gmail.com
Guest
Posts: n/a
 
      03-09-2006
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?

 
Reply With Quote
 
 
 
 
djnic djnic is offline
Junior Member
Join Date: Jul 2006
Posts: 1
 
      07-07-2006
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.
 
Reply With Quote
 
 
 
 
Sean Foy Sean Foy is offline
Junior Member
Join Date: Aug 2006
Posts: 1
 
      08-06-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help, get error "Object does not match target type" in one instanceof an app, when others work Lasse Vågsæther Karlsen ASP .Net 1 11-27-2007 02:46 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Datagrid Binding Error: "Object does not match target type." Karahan Celikel ASP .Net Building Controls 1 08-20-2003 12:57 PM
DataGrid Binding Problem : "Object does not match target type." Karahan Celikel ASP .Net Web Controls 1 08-20-2003 12:56 PM
Datagrid Binding Error "Object does not match target type" Karahan Celikel ASP .Net Datagrid Control 1 08-20-2003 10:35 AM



Advertisments