binding error message

binding error message

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


Brian Tian posted on Wednesday, December 27, 2006

Hi,  All,

I just converted my window application from .Net 1.1 to .Net 2.0 and I use Csla1.5.3 instead of old Csla1.4.x. ( I don't have time to rewrite business object to Csla2.1 now, just want my application can run under .net 2.0.) I modified my application and it compiles without errors and warnings.

But I get runtime exception when I run it. the problem is to bind the object to grid. 
Me.grdAnswerResponse.DataSource =  m_obj 

the m_obj is collection of objects and its count is 0 at first time when the form is loaded. the error message is "An exception of type 'System.InvalidOperationException' occurred in CSLA.Core.Bindablebase.dll but was not handled in user code. Additional information: Adding items not allowed"  I trace the code and find source code is at BindableCollectionBase under csla.core.bindablebase dll.

public event ListChangedEventHandler ListChanged
{
   add
   {   
if (value.Method.IsPublic && (value.Method.DeclaringType.IsSerializable || value.Method.IsStatic))
 _serializableHandlers = (ListChangedEventHandler)Delegate.Combine(_serializableHandlers, value);
else
_nonSerializableHandlers = (ListChangedEventHandler)Delegate.Combine(_nonSerializableHandlers, value);

}remove{

if (value.Method.IsPublic && (value.Method.DeclaringType.IsSerializable || value.Method.IsStatic))_serializableHandlers = (ListChangedEventHandler)Delegate.Remove(_serializableHandlers, value);
else
_nonSerializableHandlers = (ListChangedEventHandler)Delegate.Remove(_nonSerializableHandlers, value);
   }
}

The red color code will call  the below method  and throw the exception.  I checked and know the code is almost same as Cals1.4.x. Why it won't have error message popup under my application .Net1.1 even it throws exception too under csla1.4.x? I didn't modify any code for this part but I get the error under Csla1.5.3

object IBindingList.AddNew()
{
     if(AllowNew)
          return OnAddNew();
    
else
         
throw new InvalidOperationException(Strings.GetResourceString("AddItemException"));
}

The same code won't have any problem under csla.1.4.x. If the m_obj's count is not zero, it won't have the exception. How could I fix it and am I missing something? Thank you!

Brian

ajj3085 replied on Wednesday, December 27, 2006

When your list is loading in your DP_F, are you setting IsReadOnly = false before trying to add items to your collection?  Or is this not a readonly list?

Brian Tian replied on Wednesday, December 27, 2006

ajj3085,

No, it is not a readonly list, it inherits from BusinessCollectionBase(Cals.1.5.3)  I only set AllowSort = true. I can not find the detail info for the difference between csla.1.4.x and csla.1.5.3.  I thini it will help me if i can find it.

 

Thanks.

Brain

Brian Criswell replied on Wednesday, December 27, 2006

Does your collection object have this line in it?

this.AllowNew = true;

Brian Tian replied on Wednesday, December 27, 2006

No, I don't have this.AllowNew = true; I only set AllowSort = true.  Why I need this line?  BTW, when the m_obj is not empty, it is ok.

I checked my application under Csla1.4.x, it throws the exception too but there is not popup exception winodw under .Net1.1.  but under .Net2.0, it follows the some codes  and gives exception.

thanks for your help

Brian

Brian Criswell replied on Thursday, December 28, 2006

You need the line because the list implements IBindingList which has a property called AllowNew.  This tells any outside object (such as a DataGrid) whether the list allows items to be added through the IBindingList.AddNew method.  The default is false.  If you look at your example that you posted, If AllowNew is false, it will throw an exception.  So you have to add that line and override OnAddNew().  If AllowNew defaulted to true, you would get an exception because OnAddNew is not implemented by default.  VS2005 added in the new exception box and it is there whenever an exception is encountered.

xal replied on Thursday, December 28, 2006

Some grids have an issue when the collections are empty. They can't figure out what type will the child be, so they can't auto gen the columns.
As a workaround, these grids attempt to add a new item to the collection when it's empty, and then remove it.

Yeah, it sucks.

Andrés

Brian Tian replied on Thursday, December 28, 2006

Thanks all you guys' help.

 I use the Infragistic Grid control and find a similar  problem. the link is at http://forums.lhotka.net/forums/thread/1993.aspx 

Like Dawn suggestion "try not to bind the CSLA collection directly to the Infragistics Grid, using a BindSource Component instead. And set the BindSouce Componet's DataSouce to you CSLA collection." It works for me and not exception popups. but I get another problem that related to Infragistic, I may need to upgrade Infragistic. Now, I'm using Infragistic5.3.

Thanks

 

Brian

Copyright (c) Marimer LLC