Edit level mismatch in AcceptChanges - .Net - CSLA4.2

Edit level mismatch in AcceptChanges - .Net - CSLA4.2

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


gajit posted on Thursday, March 08, 2012

Hi gents,

I've reached a point where I require a parent->children business objcet.

I have a BusinessBase UserProfileEdit and a BusinessListBase UserRoles of the BusinessBase UserRole.

I used the example in the EncapsulatedInvoke project, using Order and OrderLineItem as a template and all seemed ok.

But when I make changes to my parent, and invoke this line;

mUSERPROFILEEDIT.ApplyEdit()

 

I get the "Edit level mismatch in AcceptChanges" error.

In fact,even if I don;t make changes and I invoke the CancelEdit() method, I get the same error.

I checked, and I'm definitely only invoking BeginEdit once.

 

I've had a look at other posts in the forum, but I don;t thin any give a definitie answer.

This is the full RebindUI method that saves the object:

 ' disable events
        Me.UserProfileEditBindingSource.RaiseListChangedEvents = False

        Try
            ' unbind the UI

            UnbindBindingSource(Me.UserProfileEditBindingSource, saveObject, True)

            ' save or cancel changes
            If saveObject Then

                mUSERPROFILEEDIT.ApplyEdit() ' ---- error here
                Try
                    mUSERPROFILEEDIT = USERPROFILEEDIT.Save()
                Catch ex As Csla.DataPortalException
                    MessageBox.Show(ex.BusinessException.ToString(), "Error saving", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString(), "Error Saving", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try
            Else
                mUSERPROFILEEDIT.CancelEdit() '----- also errors here
            End If
        Finally

            ' rebind UI if requested
            If rebind Then
                BindUI()
            End If

            ' restore events
            Me.UserProfileEditBindingSource.RaiseListChangedEvents = True

            If rebind Then
                ' refresh the UI if rebinding
                Me.UserProfileEditBindingSource.ResetBindings(False)
            End If

        End Try

        ApplyAuthorizationRules()

    End Sub

This doesn't happen when I remove the Childproperty from my parent object

 

This is my "Parent" userprofileEdit fetch():

    Private Sub DataPortal_Fetch(_username As String)
        Using dalManager = DataAccess.SecurityDalFactory.GetManager()
            Dim dal = dalManager.GetProvider(Of DataAccess.IUserProfileDal)()
            Using data = New Csla.Data.SafeDataReader(dal.Fetch(_username))
                data.Read()
                Using BypassPropertyChecks

                    Username = data.GetString(data.GetOrdinal("USERNAME"))
                    FullName = data.GetString(data.GetOrdinal("FULLNAME"))
                    Password = data.GetString(data.GetOrdinal("PASSWORD"))
                    PersonNo = data.GetString(data.GetOrdinal("PERSONNO"))
                    LastLogin = data.GetSmartDate(data.GetOrdinal("LASTLOGIN"), True)
                    Status = data.GetString(data.GetOrdinal("STATUS"))
                    EmailAddress = data.GetString(data.GetOrdinal("EMAILADDRESS"))

                    AddBy = data.GetString(data.GetOrdinal("ADDBY"))
                    AddDate = data.GetDateTime(data.GetOrdinal("ADDDATE"))
                    ModBy = data.GetString(data.GetOrdinal("MODBY"))
                    ModDate = data.GetDateTime(data.GetOrdinal("MODDATE"))

                    data.NextResult()
                    UserRoles = DataPortal.FetchChild(Of UserRoles)(data)

                End Using

            End Using
        End Using
        BusinessRules.CheckRules()
    End Sub

And this is my UserRoles FetchChild method:

     Private Sub Child_Fetch(data As Csla.Data.SafeDataReader)

        Dim rlce = RaiseListChangedEvents
        RaiseListChangedEvents = False
        While data.Read()
            Add(DataPortal.FetchChild(Of UserRole)(data))
        End While
        RaiseListChangedEvents = rlce

    End Sub

I used the .NextResult and FetchChild(data) method as opposed to executing a separate command, as this is what I do in CSLA2 successfully - plus I was getting issues with the conenctionmanager again.

Any ideas?

Thanks,

Graham

gajit replied on Thursday, March 08, 2012

Never mind.

I didn't realize you now need to address the binding on the children now - which we didn't do in CSLA2.

Here's the corrected RebindUI for other noobs like me: :)

Private Sub RebindUI(saveObject As Boolean, rebind As Boolean)

        ' disable events
        Me.UserProfileEditBindingSource.RaiseListChangedEvents = False
        Me.UserRolesBindingSource.RaiseListChangedEvents = False

        Try
            ' unbind the UI

            UnbindBindingSource(Me.UserRolesBindingSource, saveObject, False)
            UnbindBindingSource(Me.UserProfileEditBindingSource, saveObject, True)
            Me.UserRolesBindingSource.DataSource = Me.UserProfileEditBindingSource

            ' save or cancel changes
            If saveObject Then

                mUSERPROFILEEDIT.ApplyEdit()
                Try
                    mUSERPROFILEEDIT = USERPROFILEEDIT.Save()
                Catch ex As Csla.DataPortalException
                    MessageBox.Show(ex.BusinessException.ToString(), "Error saving", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString(), "Error Saving", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try
            Else
                mUSERPROFILEEDIT.CancelEdit()
            End If
        Finally

            ' rebind UI if requested
            If rebind Then
                BindUI()
            End If

            ' restore events
            Me.UserProfileEditBindingSource.RaiseListChangedEvents = True
            Me.UserRolesBindingSource.RaiseListChangedEvents = True

            If rebind Then
                ' refresh the UI if rebinding
                Me.UserProfileEditBindingSource.ResetBindings(False)
                Me.UserRolesBindingSource.ResetBindings(False)
            End If

        End Try

        ApplyAuthorizationRules()

    End Sub

 

Thanks,

graham

Copyright (c) Marimer LLC