-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Binding Issue
PostPosted: Wed Jul 25, 2007 12:42 pm 
Newbie

Joined: Mon Jun 18, 2007 3:44 pm
Posts: 5
Hi,

1. In a class named DBUser there is a property named FullNameByLastName.
This property does not have a backing field. It combines two backing fields
an returns a string result:


public virtual string FullNameByLastName
{
get
{
return m_LastName + ", " + m_FirstName;
}
}

2. An ObjectDataSource is used. Its SelectMethod returns a generic
collection ( List<DBUser>)

3. The ObjectDataSource is assigned/bound to a GridView.

4. FullNameByLastName is bound to a column in the GridView.

<asp:GridView ID="ListGridView" runat="server">
<Columns>
<asp:BoundField
DataField="FullNameByLastName"
SortExpression="FullNameByLastName"
HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left"
HeaderText="<%$Label:TITLE %>"
/>
Etc...

The exception I get is shown below. In brief it is: 'FullNameByLastName' on
object 'WorkforceLogic.Core.Domain.DBUser' threw the following
exception:'Object does not match target type.']

I only get this exception on class properties what don't have backing fields
such as LastNameByFullName. All other fields bind without the exception.

What do you think? Is this an issue with ObjectDataSource, GridView, or the
NHibernate? Or?

Best Regards,

--
Mike

Mike McIntyre [MVP]
http://www.getdotnetcode.com

[TargetException: Object does not match target type.]
System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
+2284281
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean
skipVisibilityChecks) +114
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object
component) +86

[TargetInvocationException: Property accessor 'FullNameByLastName' on object
'WorkforceLogic.Core.Domain.DBUser' threw the following exception:'Object
does not match target type.']
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object
component) +370
System.Web.UI.WebControls.BoundField.GetValue(Control controlContainer)
+201
System.Web.UI.WebControls.BoundField.OnDataBindField(Object sender,
EventArgs e) +60
System.Web.UI.Control.OnDataBinding(EventArgs e) +99
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +206
System.Web.UI.Control.DataBind() +12
System.Web.UI.Control.DataBindChildren() +216
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216
System.Web.UI.Control.DataBind() +12
System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32
dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState,
Boolean dataBind, Object dataItem, DataControlField[] fields,
TableRowCollection rows, PagedDataSource pagedDataSource) +221
System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding) +3004
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable
data) +59
System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
+11
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
data) +111
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,
DataSourceViewSelectCallback callback) +29
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
+69
System.Web.UI.Control.EnsureChildControls() +87
System.Web.UI.Control.PreRenderRecursiveInternal() +41
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 01, 2007 3:52 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
This is a "long-shot" but, I have been bitten in the past using fields directly, so I would try to change your property to this:

Code:
public virtual string FullNameByLastName
{
  get { return this.LastName + ", " + this.FirstName; }
}


Because of lazy loading, those fields might not have a value and perhaps your grid is complaining that the string is null??

Again, long shot but might be worth a try...


Top
 Profile  
 
 Post subject: Thanks Pelton
PostPosted: Wed Aug 01, 2007 11:38 pm 
Newbie

Joined: Mon Jun 18, 2007 3:44 pm
Posts: 5
For taking a shot an a solution.

However, it did not work.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 02, 2007 3:36 am 
Beginner
Beginner

Joined: Sun Oct 22, 2006 12:06 pm
Posts: 39
There is probably something wrong in your object.
either this.LastName or this.FirstName is null

try something this and this would print out something meaningful. Otherwise put an breakpoint into the get to see the real cause.

Code:
public virtual string FullNameByLastName
{
  get {
try{
         return this.LastName + ", " + this.FirstName; }
}catch(Exception e)
{
return "Error "+e.message";
}
}



Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 02, 2007 11:22 am 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
pyhavaim wrote:
There is probably something wrong in your object.
either this.LastName or this.FirstName is null

try something this and this would print out something meaningful. Otherwise put an breakpoint into the get to see the real cause.


Yes, you should try this advice.

I take back my statement about fields. Even with lazy loading, the fields should have a value.... The problem I had must have been something else.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 02, 2007 12:18 pm 
Newbie

Joined: Mon Jun 18, 2007 3:44 pm
Posts: 5
Strings are nullable and I have pleny of other properties that map to nullable DB fields and they come through and when finally bound to a GridView, they bind with not problems.

When I pull a list of DBUser and step through it in the debugger the FullNameByLastName property contains either a full name by last name or nothing (for those DBUser's who do not have a FirstName or LastName assigned.

When the list is bound, via a DataSource, to a GridView, the GridView throws the error I included in the first post in this series.

I keep thinking there is some other declaration or setting I need to take care of so that FullNameByLastName, which is a derived property and does not map to a DB column, will bind to the GridView.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 4:17 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
I thought I was having a case of deja vu. But it turns out I had a very similar error when I was working with the Microsoft grid on a winforms project.

Read this tread that I started and see if any of that helps you:

http://forum.hibernate.org/viewtopic.php?t=959604

Basically, all of the objects you are trying to bind to the grid must be the same type. See if you can tell if you are getting a mixture of proxy and non-proxy objects. That is my best idea for the problem you are having.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 9:20 pm 
Newbie

Joined: Mon Jun 18, 2007 3:44 pm
Posts: 5
I read that post you recommended.

I think your post has put me on the right track.

In it your wrote:

'Basically, I take the IList that is a mixture of proxy objects and my business class objects, and cast them all back to my business class type. Then, the DataGridView works fine.'


What I am trying to bind to the GridView is a List<DBUser>. The list is a mixture of columns, one exclusively defined in the business class (FullNameByLastName), the rest mapped to columns in the DB table.

The work around I came up with is:

I create a new List<DBUser> returnList.

I make a call that returns a List<DBuser> sourceList.

I iterate sourceList, creating a new DBUser for each row in that list and adding the new DBUser to the returnList. Somehow this returns a list that is bindable to a GridView whereas, the sourceList is not.

Can you show an example of the code you use to cast?

In my case, the list is returned as a List<DBUser>. I think what I may need to do is look at the data access layer and see how the returned IList is cast.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 11:21 am 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
Here is the code from my form. It is about a year old and just glancing at it I can see where I need to make improvements, but it works (for me, anyways...)

Code:
  _searchResults = Data.Search.SearchByExample<T>(_sessionNumber, _searchObject);

  System.Collections.Generic.List<T> l =
      new System.Collections.Generic.List<T>(_searchResults.Count);
  foreach (object o in _searchResults)
  {
      if (_searchObjectType.IsInstanceOfType(o))
      {
          l.Add((T)o);
      }
  }

  dgvResults.DataSource = l;


This is the code that casts from the IList to a new List<>. In my situation, this is a search box, so the grid will never be edited. That's why I can force a cast and use a List<>.

I'm not sure this would work if you want to be able to edit the objects and save them back to the database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 10:09 am 
Newbie

Joined: Mon Jun 18, 2007 3:44 pm
Posts: 5
Thank you pelton.

This looks like the code I have been using to create new objects from the list returned by NHibernate.

Maybe this is a way to avoid the extra process?


http://www.nablasoft.com/Alkampfer/?p=78


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.