Lost with BusinessListBase addnig New item to collection

Lost with BusinessListBase addnig New item to collection

Old forum URL: forums.lhotka.net/forums/t/9315.aspx


vbbeta posted on Monday, August 02, 2010

Hi to anyone who read this Post..........Im really lost !!!!!!!

fo rthe past week im trying to bind a chilLidtBase to one of its parents or it should be EditableRootList, but when ever im binding the dataGridSource to the DataItem of my parent Class and the dataSource AddNew is turned on (True) at runtime im getting this exception i have searched for it but nothing refrences the Csla Framework does anyone know how to over come this problem

 "Exception"

 AddNew cannot be called on the 'SSS.Library.Child' type. This type does not have a public default constructor. You can call AddNew on the 'SSS.Library.Child' type if you handle the AddingNew event and create the appropriate object.

 PLEASE SOMEONE HELP ME

JonnyBee replied on Tuesday, August 03, 2010

Hi,

Which version of CSLA do you use?

Add the following code to your child list class to create a new object using static factory method (orDataPortal) and add the new object to your list. :

Protected Overrides Function AddNewCore() As Object
' create an new instance of your Child
' or call DataPortal.CreateChild for create
' Dim item = DataPortal.CreateChild(Of SSS.Library.ChildType)()
Dim item = SSS.Libray.ChildType.NewChildType()
' add to list
Me.Add(item)
' return the item
Return item
End Function

vbbeta replied on Tuesday, August 03, 2010

Hi Jonny thanks for your answere

 

im using CSLA 4 i have tried yur solution but i dont know why this is not working his function is not getting Triggered at all i placed a brekpoint ant this is not being called by the UI the eception is still the same i dont know what to do

 

 

Biceman replied on Friday, August 13, 2010

Vbbeta, how were you able to override AddNewCore as OBJECT?  I'm using v4 (with VB) and the only AddNewCore available to be overridden for a BLB is explicitly typed as the items in my list.

I overroad AddNewCore and when I invoke MyList.AddNew from my code, it calls AddNewCore and everything works fine.  However, when I bind my list to a DataGridView and click in any cell of the "New Row", the exception you described occurs.  The stack trace shows the BindingSource.AddNew reporting the exception.

Am I missing something?


RockfordLhotka replied on Friday, August 13, 2010

CSLA 4 is different from previous versions, in that AddNewCore is now strongly typed. That's a good thing, or should be, because the list is strongly typed anyway, and this change means the compiler will help you avoid what would have been a runtime exception.

CSLA 4 is also different in that it provides a default implementation of AddNewCore that invokes DataPortal.CreateChild to create a new child object. In other words, it works by default, though you can always override AddNewCore if you need a different implementation.

In earlier versions of CSLA you needed to override AddNewCore if you wanted it to work - otherwise the .NET behavior is to just do "new x()" - which of course fails because typical CSLA objects have a non-public default constructor - specifically so you know you should have called DataPortal.CreateChild instead.

Biceman replied on Friday, August 13, 2010

I commented out the AddNewCore override in the BLB but I get the same error.  The List.AddNew still works when called from code and as you mentioned Rocky it calls the CreateChild  automatically. 

Regarding overriding the AddNewCore function, I was mimicking the Role/Roles classes in the ProjectTracker example.

Has anyone else seen this behavior with a DataGridView and CSLA4?

 

 

RockfordLhotka replied on Friday, August 13, 2010

Oh wait - datagridview - so web forms?

Yeah, web forms is a different thing from everything else. I don't think that the datagridview does the add operation the same way - it doesn't use IBindingList, and therefore doesn't use AddNew...

It has been too long, and I don't remember how to do an add in that control...

Biceman replied on Saturday, August 14, 2010

In my case the DataGridView is on a Win form.  It is the BindingSource that throws the error when it tries to execute "AddNew" (at the request of the DataGridView).  If I try and execute BindingSource.AddNew via code I get the same error:

{"AddNew cannot be called on the 'Test.Customer' type. This type does not have a public default constructor. You can call AddNew on the 'Test.Customer' type if you handle the AddingNew event and create the appropriate object."}

Based on the error message I researched the AddingNew event of the BindingSource.  This event fires after AddNew is called and allows you to control the creation of a new instance of your business object as such:

    Private Sub CustomerListBindingSource_AddingNew(ByVal sender As Object, ByVal e As AddingNewEventArgs) Handles CustomerListBindingSource.AddingNew

        e.NewObject = Customer.NewCustomer
    End Sub

Of course having to handle the AddingNew event for all my Binding Sources isn't ideal. 

I did try one other thing.  I changed my list to be based on BusinessBindingListBase.  The AddNewCore for this list returns a type of Object.  Now when I execute the BindingSource.AddNew no error.

After discovering this I searched the forum and found a discussion on BusinessListBase vs. BusinessBindingListBase .  This article suggests that Winforms require the use of BusinessBindingListBase.  I was modeling my code on what I saw in the ProjectTracker example which used BLB. 

Another lesson learned the hard way! Wink

 

 

vbbeta replied on Sunday, August 15, 2010

Sorry for not responding i wanted to answer you Friday rite when you asked me the question but or some reason i wasn't able to login to the csla Forums But here i am the thing what you figured out your self this is exactly what i was facing and figured that out my self as well and i wanted to answer you this

 

when you have a BLB the addNewCore cannot be as object it have to be from the class type Vs BBLB (or in csla 4 we now have DynamicListBase we could get the same work done) there you have addNewCore as object property  

Chit Swe replied on Monday, December 29, 2014

I am facing the same problem too with csla.net 4. I am trying to implement business list base type and want to bind to grid to support inline editing. But it fail to add new row in databound grid with the following Exception.

AddNew cannot be called on the 'Business.Base.ProductBrand' type. This type does not have a public default constructor. You can call AddNew on the 'Business.Base.ProductBrand' type if you handle the AddingNew event and create the appropriate object.

I also tried Roles class in ProjectTracker (csla.net 4 ) sample project. Its default implementation is to edit existing Role but not to create new Role. But if I set rolesBindingSource.AllowNew=True and try to add new role from user interface it throw the same Exception as the following. 

AddNew cannot be called on the 'ProjectTracker.Library.Admin.Role' type. This type does not have a public default constructor. You can call AddNew on the 'ProjectTracker.Library.Admin.Role' type if you handle the AddingNew event and create the appropriate object.

In this case, AddNewCore is also override in Roles class.

Do I left something to do to allow add new row in complex databinding with Editable Root Collection stereotype?

Thanks

JonnyBee replied on Monday, December 29, 2014

Hi,

Which base class does your list inherit from?

If WinForms UI then your lists should derive from the XYZBindingListBase classes. Ie: use the BusinessBindingListBase, not the BusinessListBase class.

Copyright (c) Marimer LLC