type is not serializable in CSLA 4.0

type is not serializable in CSLA 4.0

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


mrisher posted on Wednesday, December 29, 2010

I have a business object called Report that I am binding to textbox controls on a form. The class looks like this:

<Serializable()> _
    Public Class Report
        Inherits Csla.BusinessBase(Of Report)

private _description as string =  string.empty

Public Shared DescriptionProperty As PropertyInfo(Of String) = RegisterProperty(Of String)(Function(c) c.Description, RelationshipTypes.PrivateField)
        Public Property Description() As String
            Get
                Return GetProperty(DescriptionProperty, _description)
            End Get
            Set(ByVal value As String)
                SetProperty(DescriptionProperty, _description, value)
            End Set
        End Property

''code shortened to conserve space.

End Class

The binding is done like this: txtDescription.DataBindings.Add("EditValue", Report, "Description")

The Report object correctly pulls fields from the database and populates them correctly on the form via the data binding.  If a bound control on a form is blank, I type in a value and move to the next field and this is when I get the error:

"System.Runtime.Serialization.SerializationException was unhandled by user code
  Message=Type 'System.Windows.Forms.Form' in Assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

I am Using CSLA 4.0 and the error occurs in the BinaryFormatterWrapper.cs.

I have marked the form I'm using as serializable, but that didn't seem to help.  I'm very new to CSLA and was hoping someone could help.  I am glad to post more code if needed.

 

 

JonnyBee replied on Wednesday, December 29, 2010

Hi,

You probably have forms event attached to your BO and these are not serializable or you have some other reference in your BO that points back to the Form. A BO should not have any reference to the UI components.

In a WinForms app you should use the BindingSource component as an intermediate between the UI and your BO.
It seems like you are manually creating DataBinding and connecting UI controls directly to your BO?

There's several samples that show this in the samples folder, f.ex SimpleNTier.

And a Windows Form (and its events) just isn't serializable -  no matter if you add a SerializableAttribute to the form.

If you are going to save the object you must make sure to UnBind it from the UI (ie: disconnect BindingSources and BOs) before you call bo.Save().

mrisher replied on Wednesday, December 29, 2010

Johnny, thanks for your quick reply.  I am instantiating my BO in the EditValueChanged event of a list box based on what was chosen in the list box. See below.

Private Sub lstExistingReports_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstExistingReports.EditValueChanged
        Try
                          
                    Dim rpt As BusinessObjects.Report = BusinessObjects.Report.GetReportByName(lstExistingReports.Text)
                    BindControls(rpt)
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

Private Sub BindControls()
        txtName.DataBindings.Clear()
        txtDescription.DataBindings.Clear()
        txtStoredProcName.DataBindings.Clear()
        txtFormName.DataBindings.Clear()
        txtFormInstructions.DataBindings.Clear()

        txtName.DataBindings.Add("EditValue", report, "Name")
        txtDescription.DataBindings.Add("EditValue", report, "Description")
        txtStoredProcName.DataBindings.Add("EditValue", report, "StoredProc")
        txtFormName.DataBindings.Add("EditValue", report, "FormName")
        txtFormInstructions.DataBindings.Add("EditValue", report, "FormInstructions")

    End Sub

 

So this is where my problem lies?  Because like I said before, it seems like the binding works just fine.  All of the text boxes populate correctly based on the report name I choose in the list box.  I can successfully choose a different report name and the correct data populates the text boxes.   If a text box is blank (because the database field was blank), I enter in a value in the text box and as soon as the control loses focus I get the serialization error.

JonnyBee replied on Wednesday, December 29, 2010

Hi,

But if you use the Windows Forms designer and DataSources window to create your controls they will automatically be defined with databindings.

In BindControls you should only need to connect the BindingSource to your BO.

DataBinding uses a number of interfaces and expects a certain behavior so I'd prefer to use the BindingSource.

I also recommend to read the following FAQs:
DataBinding FAQ
DataGridView FAQ

These documents provide very good info on Forms and DataBinding and are available at http://www.windowsclient.net

bartol replied on Friday, December 31, 2010

The problem might also be somewhere in the code that you left out. Check if any of the properties or fields are of some type that is not serializable.  

vbbeta replied on Friday, December 31, 2010

I also faced some strange things in vb classes I would say you should copy the build DLL file from the business object project ( I believe that your doing it this way) if this dont work I would say some times it happens that you need to copy the code from the class and create a new one this code some times solve the problem. ...................... Try it have fun

Copyright (c) Marimer LLC