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

Discussion in 'ASP .Net' started by gui.besse@gmail.com, Mar 9, 2006.

  1. Guest

    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?
    , Mar 9, 2006
    #1
    1. Advertising

  2. djnic

    Joined:
    Jul 7, 2006
    Messages:
    1
    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.GetValue(Object component)

    [TargetInvocationException: Property accessor 'Name' on object 'AB' threw the following exception:'Object does not match target type.']
    System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component)
    System.Web.UI.WebControls.ButtonColumn.OnDataBindColumn(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(Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource)
    System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
    System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
    System.Web.UI.WebControls.BaseDataList.DataBind()
    System.Web.UI.Control.DataBind()
    TNS.Framework.Production.Web.Controls.SupportFinderControl.DataBind() in ...\myPage.cs:107
    TNS.Framework.Production.Web.Controls.SupportFinderControl.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.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 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.
    djnic, Jul 7, 2006
    #2
    1. Advertising

  3. Sean Foy

    Joined:
    Aug 6, 2006
    Messages:
    1
    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.
    Sean Foy, Aug 6, 2006
    #3
    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. Kevin Spencer
    Replies:
    0
    Views:
    456
    Kevin Spencer
    Jun 30, 2003
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    964
    Øyvind Isaksen
    May 18, 2007
  3. Replies:
    2
    Views:
    392
    Thommy
    Oct 29, 2008
  4. SMG

    WebServices Return DataSet + String as Objec

    SMG, Apr 22, 2005, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    200
    Ingo Eichenseher
    Apr 27, 2005
  5. Samuel
    Replies:
    8
    Views:
    121
    [MSFT]
    Oct 14, 2004
Loading...

Share This Page