DataSource thorws Object does not match target type

Posts   
1  /  2
 
    
Posts: 1251
Joined: 10-Mar-2006
# Posted on: 06-Jul-2006 23:56:44   

Put LLBLGenProDataSource on webform, attach GridView to it and it generally works.

However, I have one Entity that does not work. That entity is the root of a Target per Entity hierarchy.

When I attach it, I remove all but two fields - one is the PrimaryKey and the other is just a text field.

I then try to view the webpage - I get the following exception:



[TargetException: Object does not match target type.]
   System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) +2330229
   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) +93

[TargetInvocationException: Property accessor 'PrimaryKeyId' on object 'FullNameSpace.TheEntity' threw the following exception:'Object does not match target type.']
   System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +376
   System.Web.UI.WebControls.BoundField.GetValue(Control controlContainer) +186
   System.Web.UI.WebControls.BoundField.OnDataBindField(Object sender, EventArgs e) +60
   System.Web.UI.Control.OnDataBinding(EventArgs e) +80
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +198
   System.Web.UI.Control.DataBind() +12
   System.Web.UI.Control.DataBindChildren() +214
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +208
   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) +3012
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +12
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +101
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +25
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +140
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +68
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +61
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +67
   System.Web.UI.Control.EnsureChildControls() +97
   System.Web.UI.Control.PreRenderRecursiveInternal() +50
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5731

If I remove the Primary key field from the list of fields shown in the grid, it will display.

However, when I try to update the text field, I get the folllowing exception:

[IndexOutOfRangeException: The specified index is not in the range of known indexes] SD.LLBLGen.Pro.ORMSupportClasses.EntityFields.get_Item(Int32 index) +69 SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.CheckIfCurrentFieldValueIsNull(Int32 fieldIndex) +108 SD.LLBLGen.Pro.ORMSupportClasses.EntityField.SD.LLBLGen.Pro.ORMSupportClasses. IEntityFieldCoreInterpret.GetValue(IEntityCore entity) +96 SD.LLBLGen.Pro.ORMSupportClasses.FieldCompareValuePredicate.InterpretPredicate( IEntityCore entity) +45 SD.LLBLGen.Pro.ORMSupportClasses.Predicate.SD.LLBLGen.Pro.ORMSupportClasses. IPredicateInterpret.Interpret(IEntityCore entity) +4 SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression.InterpretPredicate(IEntityCore entity) +158 SD.LLBLGen.Pro.ORMSupportClasses.Predicate.SD.LLBLGen.Pro.ORMSupportClasses. IPredicateInterpret.Interpret(IEntityCore entity) +4 SD.LLBLGen.Pro.ORMSupportClasses.CollectionCore1.FindMatches(IPredicate filter) +174 SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase1.SD.LLBLGen.Pro.ORMSupportClasses. IEntityCollection.FindMatches(IPredicate filter) +4 SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.FindEntity(IDictionary keys) +390 SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +175 System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +78 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1218 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +853 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +87 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +117 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +86 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +153 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +172 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921

Again, this sort of thing seems to work fine with other Entities. I also tried this with Entities that are part of the same hierarchy, but are not the root and they worked as expected (both display and edit).

One other thing to note, this is the same Entity I am having another problem - I posted earlier - http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=6810

Do you have any ideas or anything else I can do to help figure this out?

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 07-Jul-2006 08:40:41   

I then try to view the webpage - I get the following exception:

This happens at design time or at runtime.

Would you please post the HTML code snippet of the GridView and DataSource setup?

And if this exception happens at runtime, would you please post the fetching and binding code?

Posts: 1251
Joined: 10-Mar-2006
# Posted on: 07-Jul-2006 18:34:57   

This happens at design time.

Keep in mind if I change the binding of the LLBLGenProDataSource to a different entity collection it works! So, it is related to the entity, not the grid/html/etc.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 07-Jul-2006 19:28:57   

WayneBrantley wrote:

This happens at design time.

Your stacktrace is from a runtime request.

Keep in mind if I change the binding of the LLBLGenProDataSource to a different entity collection it works! So, it is related to the entity, not the grid/html/etc.

It can be you have different types in the collection (polymorphic fetch) which could lead to this error when databinding: apparently the type of the object in the row is different than what the grid expects. Could you check if you have instances of the subtypes in the collection when you run the page?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 1251
Joined: 10-Mar-2006
# Posted on: 15-Jul-2006 04:55:51   

Your stacktrace is from a runtime request.

Uh....yeah. I was thinking about another one of my (many) posts wink Also, sorry for the delayed response - too many support threads running!

Could you check if you have instances of the subtypes in the collection when you run the page?

As usualy, you nailed the issue. I am trying to use the BaseEntityCollection, but it contains BaseEntity and DerivedEntity in the collection.

I looked it up in the documentation and it appears that this is supported and supposed to work - or maybe it only works in target per entity hierarchy as this is a target per entity?

Any help on what to do or how to work around this?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 15-Jul-2006 10:04:27   

WayneBrantley wrote:

Could you check if you have instances of the subtypes in the collection when you run the page?

As usualy, you nailed the issue. I am trying to use the BaseEntityCollection, but it contains BaseEntity and DerivedEntity in the collection.

I looked it up in the documentation and it appears that this is supported and supposed to work - or maybe it only works in target per entity hierarchy as this is a target per entity?

Any help on what to do or how to work around this?

Databinding in asp.net is actually pretty similar as winforms only more primitive. So if you have a collection with the factory of type A (ACollection) and you have also derived types B in the collection at runtime, which columns should the grid create? Unclear: in winforms this also gives problems, so only the base type columns are created.

This should work at runtime too: there are columns 1, 2, 3 in the grid and if a type has extra fields, that's not important, the values for 1-3 are read and shown. However if you add a column for field 4 which is only in a derived type and not in the base type, you'll get an error.

Frans Bouma | Lead developer LLBLGen Pro
BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 20-Jul-2006 03:55:33   

I am getting this as well.

I am not curtain it’s the same thing as above.

It works sometimes and then I compile and it doesn’t work.

I am using partial classes. But I can’t seem to figure out why this would error out.

I tried this using the datasource and got a similar result.

It does not seem to be the problem above becuase I am creating this in code.

One thing I can say, is when I use LLBLGenProDataSource, and I try and configure it. The EntityFactories have duplicate entries in the list. That would be my references’ are messed up. I just don't know how.

Code Behind

      Dim lUsers As New EntityCollection(Of UserEntity)(New UserEntityFactory)

      Dim lAdapter As New DataAccessAdapter

      lAdapter.FetchEntityCollection(lUsers, Nothing)

      Me.GridView1.DataSource = lUsers
      Me.GridView1.DataBind()

Error.

Object does not match target type. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Reflection.TargetException: Object does not match target type.

Source Error: 


Line 28: 
Line 29:       Me.GridView1.DataSource = lUsers
Line 30:       Me.GridView1.DataBind()
Line 31: 
Line 32:    End Sub


Source File: D:\Bryan\Projects\VisualBasic\NET2005\BAC_Solution\BAC_WebTest\Default.aspx.vb Line: 30 

Stack Trace: 


[TargetException: Object does not match target type.]
   System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) +2330229
   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) +93

[TargetInvocationException: Property accessor 'MainId' on object 'BAC.BL.EntityClasses.UserEntity' threw the following exception:'Object does not match target type.']
   System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +376
   System.Web.UI.WebControls.BoundField.GetValue(Control controlContainer) +187
   System.Web.UI.WebControls.BoundField.OnDataBindField(Object sender, EventArgs e) +60
   System.Web.UI.WebControls.AutoGeneratedField.OnDataBindField(Object sender, EventArgs e) +440
   System.Web.UI.Control.OnDataBinding(EventArgs e) +80
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +198
   System.Web.UI.Control.DataBind() +12
   System.Web.UI.Control.DataBindChildren() +214
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +208
   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) +3013
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +12
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +101
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +25
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +140
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +68
   System.Web.UI.WebControls.GridView.DataBind() +5
   _Default.Page_Load(Object sender, EventArgs e) in D:\Bryan\Projects\VisualBasic\NET2005\BAC_Solution\BAC_WebTest\Default.aspx.vb:30
   System.Web.UI.Control.OnLoad(EventArgs e) +80
   System.Web.UI.Control.LoadRecursive() +49
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3743



BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 20-Jul-2006 07:34:13   

Something else weird about this error.

1) I made a new clean project and did not add any of my own code into it. All generated code. 2) I attached a win form with a grid 3) Referenced the generated projects. 4) Loaded with the same collection. Worked Great!

Took this same project, all generated code.

1) Made a website project 2) Referenced the generated projects. Only as needed. 3) On the default page made a grid 4) On page load loaded it with the same code as above. Copied and pasted.

Did not work!

I am really baffled now.

Bryan

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 20-Jul-2006 07:35:55   

Are you defining the columns of the dataGrid? If so, would you check that you have defined columns that map to fields in the UserEntity, not in another subType (if it happens to exist).

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 20-Jul-2006 09:59:57   

Also, as I said above: webforms binding is much more primitive than winforms binding, apples and oranges you can't compare them. they use completely different type definition structures etc. so testing it with winforms is great, but it won't tell you if it will work in webforms.

Frans Bouma | Lead developer LLBLGen Pro
BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 20-Jul-2006 10:10:09   

Walaa wrote:

Are you defining the columns of the dataGrid? If so, would you check that you have defined columns that map to fields in the UserEntity, not in another subType (if it happens to exist).

I have recreated it and tried it many times.

I have tried it where the fields get created for you using the datasource control and I have tried it where a blank grid is assinged one in code.

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 20-Jul-2006 10:12:21   

Otis wrote:

Also, as I said above: webforms binding is much more primitive than winforms binding, apples and oranges you can't compare them. they use completely different type definition structures etc. so testing it with winforms is great, but it won't tell you if it will work in webforms.

Sure, do you have any ideas.

Its just a blank project. Another wierd part is it has worked before. There has to be something that was generated that caused this to fail and only fail in the web project.

And I have regenerated many times. There are no changes.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 20-Jul-2006 12:01:06   

BringerOD wrote:

Otis wrote:

Also, as I said above: webforms binding is much more primitive than winforms binding, apples and oranges you can't compare them. they use completely different type definition structures etc. so testing it with winforms is great, but it won't tell you if it will work in webforms.

Sure, do you have any ideas.

Its just a blank project. Another wierd part is it has worked before. There has to be something that was generated that caused this to fail and only fail in the web project.

And I have regenerated many times. There are no changes.

This in general happens when ASP.NET 2.0 sees an instance of 'SubType' when it expects 'SuperType'. E.g. you have a collection of Employee bound to the grid, but the collection you bound also contains instances of subtypes of Employee, e.g. Manager. When manager is seen, this error is given by asp.net. Is that the case in your situation?

Frans Bouma | Lead developer LLBLGen Pro
BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 20-Jul-2006 18:17:54   

It is not the case at all.

      Dim lUsers As New EntityCollection(Of UserEntity)(New UserEntityFactory)

      Dim lAdapter As New BAC.BL.DatabaseSpecific.DataAccessAdapter

      lAdapter.FetchEntityCollection(lUsers, Nothing)

      Me.GridView1.DataSource = lUsers
      Me.GridView1.DataBind()


This code above works great until we get to the grid. I even visually debug the collection. How can it think there is something wrong with it if I can see the collection. The grid should just fill what is there. Its a blank grid.

Here is the page code. There are no columns in there. It's a blank grid.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="SD.LLBLGen.Pro.ORMSupportClasses.NET20" Namespace="SD.LLBLGen.Pro.ORMSupportClasses"
    TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 20-Jul-2006 21:14:07   

Found out a lot more about this.

I regenerated the project a bunch more times and tried a ton more scenarios.

It seems to be a problem when I rename Entities in the LLBL designer.

I just took 2 tables. Tables “obj” and “objUser”.

When I use the entityname strip pattern. {obj}{} And when I rename the obj entity to “Object”. The above scenario happens.

So it’s something to do with the renameing and or strip pattern.

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 20-Jul-2006 23:54:36   

This above does not seem to be the answer either. Now this is not working. I have not changed the code at all. It works then it doesn't. Very frustrating.

If I remove the subtype, the supertype works, when I add the subtype, the supertype then stops working.

How can I determine if I am setting this up wrong?

Clearly the code below is not confusing the types.

   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

      Dim lUsers As New BAC.BL.HelperClasses.EntityCollection(Of _
               BAC.BL.EntityClasses.ObjUserEntity)(New BAC.BL.FactoryClasses.ObjUserEntityFactory)

      Dim lAdapter As New BAC.BL.DatabaseSpecific.DataAccessAdapter

      lAdapter.FetchEntityCollection(lUsers, Nothing)

      Me.GridView1.DataSource = Nothing
      Me.GridView1.DataSource = lUsers
      Me.GridView1.DataBind()

   End Sub

   Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

      Dim lObjects As New BAC.BL.HelperClasses.EntityCollection(Of _
               BAC.BL.EntityClasses.ObjEntity)(New BAC.BL.FactoryClasses.ObjEntityFactory)

      Dim lAdapter As New BAC.BL.DatabaseSpecific.DataAccessAdapter

      lAdapter.FetchEntityCollection(lObjects, Nothing)

      Me.GridView1.DataSource = Nothing
      Me.GridView1.DataSource = lObjects
      Me.GridView1.DataBind()

   End Sub
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 21-Jul-2006 09:45:04   

Please answer this for me: do you or do you NOT have supertypes AND subtypes in ONE collection? That was my question, and you answered NO, however your last post suggests you DO. Please confirm this: do you bind a collection with objects of multiple types to the grid (e.g. supertype and subtype)? If so, that's the problem.

Calling an entity 'Object' is not a good idea, as that's the base class for all objects in .NET.

Visually debugging the collection uses winforms databinding, which isn't dumb as asp.net's databinding is. (if you used the debugger visualizers.)

The thing is: if you have 2 objects in 1 collection and one is the type B (subtype) and the other is type A (supertype), the grid will reflect the first and create columns for that type. it will then bind the object to the columns and all is well. Then it moves on to the next type and it sees a different type than the previous: it then throws this error.

At least that's my understanding of the cause of the error you ran into, which has been reported before and what I described above was the reason for it.

In winforms, the ITypedList implementaton on EntityView(2) filters out the columns of the subtypes, so that works OK, but in asp.net 2.0 this apparently is ignored.

I can understand you must be deeply frustrated by this and with all the frustration I've been through with the datasourcecontrols I can deeply relate to that frustration, but please understand that trying to solve this with the information provided in this thread (not your fault, this just happens to be the case in these kind of threads) is kind of a gamble: we try to recognize patterns in the information provided and based on that think this or that is the cause, but we might be wrong in some situations, and that of course only adds to the frustration of the customer who just has a problem and wants it solved a.s.a.p.

In general I hate the idea of asking customers to write a repro case, as it's often time consuming and chances are the repro case might not reflect the issue at hand. However, if you don't have 2 or more types in the same collection you're fetching (i.o.w.: if User doesn't have SUBtypes, the collection won't have more than 1 typed objects in it), it's another problem causing this error, something I haven't seen before.

Frans Bouma | Lead developer LLBLGen Pro
BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 21-Jul-2006 19:05:26   

First I would like to say thanks for taking the time to respond. It’s hard to show how grateful you are for the help as you write down and discuss your problem. Problems are frustrating and are sometimes read as angry and I am far from it.

Otis wrote:

Please answer this for me: do you or do you NOT have supertypes AND subtypes in ONE collection? That was my question, and you answered NO, however your last post suggests you DO. Please confirm this: do you bind a collection with objects of multiple types to the grid (e.g. supertype and subtype)? If so, that's the problem.

I don't think I know enough to answer this question. I have given all the code that I have written above.

If I had to guess at the answer, which is what I have to do, I would say that I am not putting two types in one collection. I was 90% sure that was the case. But after reading your latest post and seeing how adamant you are about what I have written above, I am now unsure. I am not an LLBL expert.

Basically what I understand to be supertypes AND subtypes in ONE collection is if I where to fetch the supertype and then fetch the subtype into the same collection then try to load it into a grid. If that is what you meant, then clearly with the code that I have written that is NOT what I am doing.

I have provided all the code in the page and the page behind. The ASPX side only has a GRID and the code behind has 2 button events. One event creates a NEW collection of “Objects”, which is the supertype, then fletches them and then assigns it the BLANK GRID(Which has NO COLUMNS ASSIGNED) and displays it. The other does the same with the “User” entity collection and it is a subtype of “Object”. Before each event the datasource is cleared and the collection is recreated with the NEW statement. Also, you might think that the viewstate is getting in the way. That is not the case because I have tested it after I load the page for each event. Basically I could have the events on separate pages and it made no difference. This page was created for testing purposes. If you want I can supply 2 different pages with to blank grids with one button and event on the back, while loading each the object collection on one page and the user collection on the other. This would mean there would be no way for any other entity to get into the collection. This problem will and does still happen under that case as well. Maybe by adding them onto the same page it has messed up my example. Let me know if that is what you want me to do, because the same result will be had.

When I generate in the LLBL designer both the Object and the User entities and make the user a subtype, only the “User” collection will work with this above scenario. If I regenerate and remove the “User” from the project and the generation, then the “Object” event will work. I can’t get them both to work on the same page or on separate pages. The same page thing was just because I was doing it fast. I really think it has made this example confusion and I am sorry.

The only thing I can think of, that would match what you are saying is really weird. Which is why I don’t think you are saying it, but I will try. I really don’t think this, but with how adamant you are I guess this could be the case. Are you saying that since “Object” is a supertype of “User”, that it can never be bound to an entity collection and display ever because it has a subtype. That would be odd and I don’t think this is correct.

Anyway, I think what you meant was that first scenario and CLEARLY with my statements and my code that I am not loading to different entities into one collection. You have all the code. I will supply 2 different pages and code behinds if you like.

Otis wrote:

Calling an entity 'Object' is not a good idea, as that's the base class for all objects in .NET.

Are you saying the generated code will not work correctly when I name that Entity, “Object”? I guess they like the name as much as I do.

Otis wrote:

Visually debugging the collection uses winforms databinding, which isn't dumb as asp.net's databinding is. (if you used the debugger visualizers.)

I only mentioned this because I thought it would help you eliminate, other possible scenarios in your head. It was only meant to help, and I have heard and read your comments on how different it is then ASP.NET. So it was only to inform and help us during the debug of this problem.

Otis wrote:

The thing is: if you have 2 objects in 1 collection and one is the type B (subtype) and the other is type A (supertype), the grid will reflect the first and create columns for that type. it will then bind the object to the columns and all is well. Then it moves on to the next type and it sees a different type than the previous: it then throws this error.

I believe with all my heart that this is not the case. I will show you 2 different pages one with the “object” and one with the “user” They are super and sub types, but they are not fletched into the same collection.

Maybe you mean that the data in the underlining tables has items in both? Which of course you would have items in both in the data. That only makes since though. I mean the actual SQL Server 2005 data tables.

Otis wrote:

In winforms, the ITypedList implementaton on EntityView(2) filters out the columns of the subtypes, so that works OK, but in asp.net 2.0 this apparently is ignored.

I am not loading 2 entities into one collection. As far as I know. Maybe you can show me an example of someone doing this. My example above does not show this. Or if it does, I have know idea how.

Otis wrote:

I can understand you must be deeply frustrated by this and with all the frustration I've been through with the datasourcecontrols I can deeply relate to that frustration, but please understand that trying to solve this with the information provided in this thread (not your fault, this just happens to be the case in these kind of threads) is kind of a gamble: we try to recognize patterns in the information provided and based on that think this or that is the cause, but we might be wrong in some situations, and that of course only adds to the frustration of the customer who just has a problem and wants it solved a.s.a.p.

I feel your pain with this. I have done something similar with a datagrid. Basically I send the grid a view and it does everything. Many frustrating hours, but its code I have used and liked since a year before the first beta of .net.

Otis wrote:

In general I hate the idea of asking customers to write a repro case, as it's often time consuming and chances are the repro case might not reflect the issue at hand. However, if you don't have 2 or more types in the same collection you're fetching (i.o.w.: if User doesn't have SUBtypes, the collection won't have more than 1 typed objects in it), it's another problem causing this error, something I haven't seen before.

I think its something new.

Thanks for your help.

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 21-Jul-2006 21:27:22   

Full Aspx Page Code for the single test of filling an "ObjectEntity" which is the supertype of the "UserEntity" into a collection that shows to a grid.


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TEST_Object.aspx.vb" Inherits="TEST_Object" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>


Full Code Behind


Partial Class TEST_Object
    Inherits System.Web.UI.Page

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim lObjects As New BAC.BL.HelperClasses.EntityCollection(Of _
         BAC.BL.EntityClasses.ObjEntity)(New BAC.BL.FactoryClasses.ObjEntityFactory)

      Dim lAdapter As New BAC.BL.DatabaseSpecific.DataAccessAdapter

      lAdapter.FetchEntityCollection(lObjects, Nothing)

      Me.GridView1.DataSource = Nothing
      Me.GridView1.DataSource = lObjects
      Me.GridView1.DataBind()
   End Sub
End Class

This code works when I remove the subtype user in the LLBL Designer generation studio. Then I delete the generated projects and regenerate them. This above will show a grid with the objectentity collection and all its data.

This code will fail if I go to the designer and add the objUser table as a UserEntity then make it a subtype of the ObjectEntity.

This seems wierd becuase as you can see in the code I am in no way adding the UserEntity to the collection. I don't reference it anywhere in the code on the aspx page or the code behind. Why would the subtype interfere at all with this code? Again this page does not work when I regenerate the projects after adding the "User" subtype.

I am doing my best to make this clear. Let me know what I need to do to get you a sample or something.

I am going to add the other psot that tests the UserEntity and filling it. Trying not to confuse the issue.

This is a reproducible error at this point.

Bryan

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 21-Jul-2006 21:31:48   

ASPX Code for the UserEntity collection test page


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TEST_User.aspx.vb" Inherits="TEST_User" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>


Code behind.



Partial Class TEST_User
    Inherits System.Web.UI.Page

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim lUsers As New BAC.BL.HelperClasses.EntityCollection(Of _
         BAC.BL.EntityClasses.ObjUserEntity)(New BAC.BL.FactoryClasses.ObjUserEntityFactory)

      Dim lAdapter As New BAC.BL.DatabaseSpecific.DataAccessAdapter

      lAdapter.FetchEntityCollection(lUsers, Nothing)

      Me.GridView1.DataSource = Nothing
      Me.GridView1.DataSource = lUsers
      Me.GridView1.DataBind()
   End Sub
End Class


Now this works. There is no problem with this code.

It’s just when I add this UserEntity as a subtype in the LLBL designer and regenerate the code that the ObjectEntity collection example above does not work.

confused

If these last 3 posts are not clear, I need to have some help as to telling you what I need to give you. I am at a loss. I am trying my best to detail this out as clear as I can.

Bryan

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 22-Jul-2006 01:29:12   

I am hoping this is not the problem

Lets say I have 3 tables

Table Name - Fields ObjectTable - MainID, Name, DateCreated ObjectUserTable - MainID, UserName, Password ObjectOrder - MainID, OrderDate, Address1, City,State,Zip

Lets say I set up my Entities in the studio so.

SuperType - > SubType -> Entity Name ObjectTable - > ObjectUserTable -> UserEntity ObjectTable - > ObjectOrder - OrderEntity

Then I add 1 UserEntity and 1 OrderEntity and save them to the DB

The 3 tables would have the following record counts.

ObjectTable - 2 records, 1 for the order 1 for the user. ObjectUserTable - 1 for the 1 entry added. ObjectOrder - 1 for the one entry added.

This is how I have been doing it for a long time. Is this what you are talking about.

I would guess the the UserEntity only pulls records from the ObjectTable and USerTable where they both match.

Anyway Just trying to solve this.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 22-Jul-2006 09:37:58   

This is how I have been doing it for a long time. Is this what you are talking about.

From the first look of it, yes this is what I meant with 2 types in 1 collection bound to a grid. Though I have enough info now to try to repro it with a structure similar to yours. So we'll soon find out if this indeed is the case.

It doesn't matter if you bind a collection with subtypes, it matters if you have 2 types in the collection, e.g. objectentity and userentity.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 22-Jul-2006 12:12:09   

BringerOD wrote:

Full Aspx Page Code for the single test of filling an "ObjectEntity" which is the supertype of the "UserEntity" into a collection that shows to a grid.


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TEST_Object.aspx.vb" Inherits="TEST_Object" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>


Full Code Behind


Partial Class TEST_Object
    Inherits System.Web.UI.Page

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim lObjects As New BAC.BL.HelperClasses.EntityCollection(Of _
         BAC.BL.EntityClasses.ObjEntity)(New BAC.BL.FactoryClasses.ObjEntityFactory)

      Dim lAdapter As New BAC.BL.DatabaseSpecific.DataAccessAdapter

      lAdapter.FetchEntityCollection(lObjects, Nothing)

      Me.GridView1.DataSource = Nothing
      Me.GridView1.DataSource = lObjects
      Me.GridView1.DataBind()
   End Sub
End Class

This code works when I remove the subtype user in the LLBL Designer generation studio. Then I delete the generated projects and regenerate them. This above will show a grid with the objectentity collection and all its data.

This code will fail if I go to the designer and add the objUser table as a UserEntity then make it a subtype of the ObjectEntity.

This seems wierd becuase as you can see in the code I am in no way adding the UserEntity to the collection. I don't reference it anywhere in the code on the aspx page or the code behind. Why would the subtype interfere at all with this code? Again this page does not work when I regenerate the projects after adding the "User" subtype.

I think I have to explain something, which will make it clear simple_smile

You have this hierarchy: Object <- User

When you fetch a collection of Object, also the User entities are loaded, not only the Object entities. This is because User is-a Object, due to inheritance.

So your code above loads Object entities, but it also loads User entities. Here it goes wrong with the grid, as it then suddenly runs into a UserEntity type, which it didn't expect and it gives up.

If you want to have solely Object entities in the collection and NOT any subtypes, you've to add a filter to which you add Type filters (see: Using the generated code -> Adapter -> Filtering and sorting -> Advanced filter usage -> Filtering on entity type, and you've to make the filters negative, so that they will be true if the type is NOT UserEntity for example.

However, due to inheritance, a UserEntity is of type ObjectEntity, so if you fetch a collection of ObjectEntity instances, you of course should get UserEntity objects as well.

The grid gives up, because potentially, there are more columns in UserEntity than in ObjectEntity. This is what I was trying to explain in my previous posts, I forgot that it might not be clear that if you fetch a collection of the supertype, the subtypes are fetched as well.

Now, why does it work in winforms? In Winforms, the bound grid asks the collection which properties are present in the contained objects. I added code to the entityviews (and previously to the collections) which simply reports the properties of the supertype, not the subtypes, so this will work in winforms. In webforms, that interface (ITypedList) is ignored and therefore this code isn't called unfortunately.

I haven't tested but I can assume what will solve it is that you add the columns at design time and switch off autogenerate columns in the grid properties. This will make the grid simply ask the returned entity view for the properties it KNOWS (which you designed), it doesn't have to reflect over the types in the bound object.

I hope this explains what's going on. I'll now re-read your other posts to see if I have overlooked something.

Frans Bouma | Lead developer LLBLGen Pro
BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 22-Jul-2006 18:33:53   

Thanks for your continued support and help. I really appreciate it.

Otis wrote:

You have this hierarchy: Object <- User

When you fetch a collection of Object, also the User entities are loaded, not only the Object entities. This is because User is-a Object, due to inheritance.

So your code above loads Object entities, but it also loads User entities. Here it goes wrong with the grid, as it then suddenly runs into a UserEntity type, which it didn't expect and it gives up.

This seems really weird. The relationship in the database and the relationship I desire are for each User to have at least 1 record in Object, but not for Object to have at least one relationship in User. The Object table via its name and purpose can hold many different object types, and it does. It should not attempt to load any of the objects.

Also, why would the ObjectEntity collection default load using an ADAPTER(which does not use lazy loading) pull up the UserEntities or any other entity? The adapter does not use lazy loading. When you load the ObjectEntity it should not and I have seen does not in the collection load the UserEntity. I don’t see it load the UserEntity in the visualizer. What gives?

Another thing, I have tested this with other subtypes from Object and this works fine. It basically only happens with the User table. At this moment I am going to start eliminated things to determine what the cause is. Renameing the User table, deleting the data in it, removing columns. I think this tactic will garner results. I might find that a field name was the issues. Or an index, but I have already renamed the table and rebuilt the indexes, so its got to be data in the table or fieldnames at this point.

Object <- User ** Does not work, makes the Supertype fail Object <- Application **Works, both work, of course have to remove the user entity Object <-Group ** both work

You get the idea; the USER subtype is the problem. Once I remove it, your above statements don’t affect me. Meaning they don’t make sense with the tests I have run because I run an ApplicationEnity collection fine and the ObjectEntity collection fine, when the UserEntity is removed from the designer.

Just to let you know I have deleted rename and removed and re-added indexes and primary keys to double check them.

This is a really odd one.

Bryan

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 22-Jul-2006 19:49:49   

OMG

I think I have figured it out.

I should get a cracker or something. ?

I hope I have figured it out.

Hard to explain but I will give it a shot.

The Object table has many records in it. It has User records Application records, group records, etc.

It seems what ever record is first in the table is what one it tries to call up. It just so happens that the first record in the object table has an ID of 2, which is a user record. I changed the first record to 9 which is the application table and now it is switched to that table record entity with the problem.

This is even true with a sort direction. If I set a sort on the main id of the object table descending I get the error if the first result item is a subtype that is user again and now the user table is the problem.

So you are right it seems. But if you are right then LLBL seems to be very flawed, or I just don’t understand the benefit of this if it’s a feature.

Here is why I don’t understand the benefit. If its going to pull up its subtypes from a regular supertype entity collection, why does it only check the first record then non of the rest of them. If by design the system will check the subtypes, then just because the first record does no contain a subtype does not mean the second record doesn’t. It could with regular RDMS rules have many subtypes.

Also, if the first record that gets returned does not have a subtype then the all things seem to work without error. So the first record say has id 1 and has no subtype related to it, but the seconds record is id 2 which has a subtype user related to it the system works. This means that it only checks once and only the first record. Seems wierd to me and not very usefull. I am sure I am wrong here, I am sure I am missing the point. I in no way mean to offend with my words. I just don't get it.

Also, why does it pull up related entities when I am using the adapter model. I don’t want any other queries being run in this scenario. I just want “select * from object”. It seems to be doing that and when it hits the first record calling a “select * from user”.

Anyway, can I turn this off? I have MANY entities that are subtypes of the Object Entity.

Bryan

1  /  2