StrataFrame Forum

AllowNullValuesOnNewRow problem

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

By Sam Tenney - 12/8/2010

Business objects have a property named AllowNullValuesOnNewRow which defaults to False.  The StrataFrame help system says "When True, the NewRow() function will automatically initialize the row to prevent any DBNull values from occurring." My first problem is that I think the help system explanation is probably wrong.  I think it should say "When False, the NewRow() function will automatically initialize the row to prevent any DBNull values from occurring." My second problem occurs when I use a non-nullable char data type as the primary key in my SQL table.  When I click the New button on a MaintenanceFormToolStrip to create a new record in that table, I get an error because the NewRow() function does NOT automatically initialize the primary key value to "" regardless of how I set the AllowNullValuesOnNewRow property.  The value of the primary key (which should be initialized to "" as a string) remains as DBNull which causes the error. There must be a reason why primary keys are ignored when initializing new row values, but I cannot imagine what that reason is.  Is this a bug or can somebody help me understand why the framework works this way. Sam Tenney
By Ivan George Borges - 12/8/2010

Hi Sam.

To be honest with you, I have never tried a string field primary key with SF, I always go to the Integer and Auto-incremented enabled, this has never failed.

I will try and test it later.
By Edhy Rijo - 12/8/2010

Hi Sam, Ivan,
I have an application which use GUID for the Primary Keys with a Varchar(32) field.  In this case, this application is designed for multi-office or branches in mind and the original VFP application was also based on GUID PKs which make sense to us and not to worry for duplicate keys.

Here is what I did:

  1. I use a base BO class and set the following properties:
    • PrimaryKeyIsAutoIncement = False
    • PrimaryKeyIsUpdatable = True
  2. In my base BO class I overrided the OnSetDefaultValues() to automatically generate the GUID if the above properties are with those set values, here is the code:

    ''' <summary>
    ''' Check the values for the PK field to be set to a GUID 32 char value.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub OnSetDefaultValues()
        MyBase.OnSetDefaultValues()

        If Me.PrimaryKeyIsUpdatable = True AndAlso Me.PrimaryKeyIsAutoIncremented = False Then
            '-- Set Value for GUID PK 
            '-- Get a reference to the property descriptor (which doesn't use reflection)
            Dim loFieldPropDescriptor As FieldPropertyDescriptor = Me.GetPropertyDescriptor(Me.PrimaryKeyField)

            '-- If the PK type is a Guid then Generate a Sequential GUID to prevent Index fragmentation
            If loFieldPropDescriptor.PropertyType Is GetType(System.StringThen
                loFieldPropDescriptor.SetValue(Me, ezCallTrack.Basics.NewSequentialID)
            End If
        End If
    End Sub


Sam, I hope this logic can help you in any way.
By Sam Tenney - 12/8/2010

Thanks Ivan and Edhy,

The primary key in my case is pre-defined as a non-nullable char field and cannot be changed without upsetting the apple cart.  I have found a workaround by adding code to set the primary key value to "" in the SetDefaultValues method in the business object, but I would still like to know why StrataFrame code intentionally ignores primary key values when initializing all the other values in the business object.

And do you agree that the wording in the help system needs to be reversed?

Sam Tenney
By Ivan George Borges - 12/10/2010

Hey Sam.

Here is something about the property:

http://forum.strataframe.net/FindPost2492.aspx
By Sam Tenney - 12/10/2010

Hi Ivan,

Unfortunately the post you referenced just confuses the issue even more.  In that post, Trent explains that "You have the AllowNullValuesOnNewRow property set to False on a business object, it will automatically initialize the row for you."  That statement agrees with what I believe is correct, but it disagrees with the StrataFrame help system which says referring to that property, "When True, the NewRow() function will automatically initialize the row to prevent any DBNull values from occurring."  But then just to confuse me even more, Trent suggests setting the property to False which is what he said it was already set to.  I am sure this was just a human mistake in typing, but I still don't know which statement is correct, Trent and me or the help system.

And no matter which statement is correct, I have tried both settings and still the primary key value is not being initialized and it remains set to DBNull which causes an error when the StrataFrame code merely tries to access the primary key value and cast it as a string and that happens long before attempting to Save to the database table.

For some unknown reason, the StrataFrame code is skipping past the initialization process for primary keys when adding new records.  I expect there is a good reason for doing that but nobody has been able to explain it to me yet.  And by the way, it looks to me like it would skip past initializing any primary key even if it was not a string type and the error would occur as long as the primary key was to be manually entered by the end user.  And remember, the error occurs before the primary key value is even displayed in the textbox.

Just try it, I don't think you will like it.

Sam Tenney