StrataFrame Forum

IsDirty Question?

http://forum.strataframe.net/Topic14071.aspx

By fansanitis - 2/6/2008

Whenever I do a BO.Add() to my business object the IsDirty flag is being turned on even though no bound UI controls have been changed.  I'm not sure but I believe its because I have several fields in the BO that have Custom Field Properties set.  The underlying table does NOT allow nulls on any field but the user is not required to enter all fields.  So for the fields that are not required I set the Null Value Option and Null Replacement value as follows:

Strings: Return Alternate On Null/Set Null On Alternate(reference type) -- String.Empty

Integers: Return Alternate On Null/Set Null On Alternate(value type) -- 0

DateTime: Return Alternate On Null/Set Null On Alternate(reference type) -- #1/1/1800#

The getting/setting of the values works fine but is that what is causing the BO to be flagged as dirty when a new record is added?  If so, how do I get around this problem?  I have a simple data entry app that clears the BO on each successful BO.Save and then adds a new record using BO.Add.  When the user closes the app, they're always prompted with the "Save Changes" message dialog since the BO is flagged as dirty.

Thanks!!

By Greg McGuffey - 2/6/2008

This is the normal behavior. It took me a while to wrap my head around it, but what happens is that when you add a new record, the underlying DataTable has a row added to it. That row is marked as dirty (via normal ADO.NET mechanisms). Because DataTables are disconnected, it is hard to tell when, exactly, a row changed from "just added, but no user changes" to "dirty because the user changed something". This is really an ADO.NET thing.



Some other posts on this subject:

some background...

http://forum.strataframe.net/FindPost9401.aspx



about this very issue...

http://forum.strataframe.net/FindPost7177.aspx



I know this doesn't exactly answer your question, but hopefully understanding what's going on will help you start to figure something out.



As to how to know if the user has entered any data, you might look into using property changed events to flag that the user has entered something, then check that flag when the form closes. If they haven't made any changes, you could simply Undo the changes on the BO before closing.



Good luck!
By Trent L. Taylor - 2/6/2008

Greg's references are good...if this doesn't get you going let us know.  Thanks. Smile
By fansanitis - 2/6/2008

I added a Evaluations.CurrentDataTable.AcceptChanges() statement immediately after each Evaluations.Add() to accept the changes to the table but not the database.  This gaves me Original datavalues that I can compare to the Current datavalues to see if anything has changed.

Here's some code I placed in the FormClosing event:

Dim ix As Integer = Evaluations.CurrentDataTable.Columns.Count

Dim isDirty As Boolean

For ix = 0 To Evaluations.CurrentDataTable.Columns.Count - 1

   If Evaluations.CurrentRow.Item(ix, Data.DataRowVersion.Original) <>

      Evaluations.CurrentRow.Item(ix, Data.DataRowVersion.Current) Then

         isDirty = True

         Exit For

   End If

Next

If isDirty Then

   Dim dlgResult As DialogResult

   dlgResult = MessageBox.Show("Do you want to save changes?", My.Application.Info.Title, _  

      MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button3)

        Select Case dlgResult

          Case Windows.Forms.DialogResult.Cancel

             e.Cancel = True

          Case Windows.Forms.DialogResult.Yes

             If Evaluations.Save() <> MicroFour.StrataFrame.Data.SaveUndoResult.Success Then

                 e.Cancel = True

             End If

          Case Windows.Forms.DialogResult.No

             '--Do nothing

       End Select

This seems to work but is this a proper way of getting what I need or am I opening myself up for future problems?

Thanks!! 

 

By Trent L. Taylor - 2/6/2008

This seems to work but is this a proper way of getting what I need or am I opening myself up for future problems?

No, I think you are good. Smile