I have a business object called Report that I am binding to textbox controls on a form. The class looks like this:
Public Class Report
Inherits Csla.BusinessBase(Of Report)
private _description as string = string.empty
Shared DescriptionProperty As PropertyInfo(Of String) =
RegisterProperty(Of String)(Function(c) c.Description,
Public Property Description() As String
Return GetProperty(DescriptionProperty, _description)
Set(ByVal value As String)
SetProperty(DescriptionProperty, _description, value)
''code shortened to conserve space.
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=184.108.40.206, 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.
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().
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
Dim rpt As BusinessObjects.Report = BusinessObjects.Report.GetReportByName(lstExistingReports.Text)
Catch ex As Exception
Private Sub BindControls()
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")
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.
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.
These documents provide very good info on Forms and DataBinding and are available at http://www.windowsclient.net
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.
Copyright (c) Marimer LLC