StrataFrame Forum

How do I set a default value?

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

By Peter Jones - 2/21/2007

Hi,

I'm obviously missing something pretty basic here but I can't see it. I have a table with a GUID as a PK. When I save a new record I get the error:

BusinessLayerException
 An error occurred while saving an the data to the server.
DataLayerSavingException
 Cannot insert the value NULL into column 'AGTID', table 'TMS201.dbo.tblAGTAttrGroupTypes'; column does not allow nulls. INSERT fails.
The statement has been terminated.
SqlException
 Cannot insert the value NULL into column 'AGTID', table 'TMS201.dbo.tblAGTAttrGroupTypes'; column does not allow nulls. INSERT fails.

However, in the BO I have the field set as per screen shot - note the screen shot also shows the property code for the column. I have tried setting the bo control value to "Return Alternate on Null" but the result is the same.

I also noticed that, when I create a new row, a checkbox for which I have a default value of 'True', is unticked on the new record. I was presuming that the defaults settings in the BO would take effect when a new is presented to the user for initial data entry - is this not so?

BTW my test form is using a DevEx grid.

Cheers, Peter

By Greg McGuffey - 2/21/2007

Peter,



I believe I've seen in other posts that you'd normally set the GUID in the SetDefaultValues event. There is a stub created for this event handler when you create a BO.
By Peter Jones - 2/21/2007

Hi Greg,

Thanks for the comment. I wonder then, if the developer has to hand code defaults what purpose does the "null value option" serve - maybe I've misunderstood how that is used within SF.

Cheers, Peter

By StrataFrame Team - 2/22/2007

Greg is right on the SetDefaultValues() method.  For GUID pks, you either have to set NEWID() as the default value for the column within SQL Server (but the newly assigned GUID will not be retrieved on a save), create a stored procedure for INSERTs that will return the new GUID in an output parameter, or set the AGTID field within the SetDefaultValues() method to Guid.NewGuid(). 

As for the "Null value options" they are in place because ADO.NET represents null database values with DBNull.Value, and the DBNull type cannot be converted to any other data type (save object), and so when you access the get of a field property that has a null, you will get an InvalidCastException.  So, if you want your database to allow null values, then you will need to configure the fields that you want to allow null values to return some other value when a DBNull.Value is encountered.

By Peter Jones - 2/22/2007

Hi Ben,

Ok - I think I understand that (I need to do a bit more reading) but one question at this stage. So, I have a db field that is null and I use Null Value Option to cover the Get of an existing row, e.g. maybe String.Empty I suppose for a varChar field, Nullable Generic from other data types. However, what happens on the Save - I have presented the data to the user who has not entered anything into that field and, consequently, I want to maintain the field's original Null value. Does this happen automaticaly on the Save?

Cheers, Peter

By StrataFrame Team - 2/23/2007

Yes, this happens automatically on a save... the NULL value within the business object is persisted back to the database.  However, the database table has to be configured to accept NULL values... and a primary key field cannot accept NULL values.
By Peter Jones - 2/25/2007

Hi,

Well I tested this out and it didn't seem to work. I have a field AGTName which allows Nulls and has no defaults. If I manually create a row in the database and don't key anything for AGTName the new row has a Null for this value. In my BO I set the Null Replacement Value to string.empty and I have no default set up for this column. In the Null Value Option property I have tried "Return Alternate on Null" plus the two new variations introduced in 1.6 and, whenever I create a new record and don't key anything into AGTName, I get an empty (not a Null) value in the database.

While on this subject can you please clarrify what the two new "Return Alternate on Null / xxx" properties do. I was assuming that if I select "../ Set Null on Alternate (reference type)" then, if the BO sees that the value being saved is the same as the value in "Return Alternate to Null" (in my case string.empty), then it would propergate Null back to the database. Is this correct?

Cheers, Peter

By StrataFrame Team - 2/26/2007

The problem isn't with your configuring of your field.  A business object has a "blanket" property that prevents any NULL values from new records within the business object.  It's called AllowNullValuesOnNewRow, and it defaults to False.  When it's False, the business object will initialize all columns of a new row to be the .NET default values for the data types (except System.String, which is set to String.Empty rather than Nothing (C# null)).  So, if you set that AllowNullValuesOnNewRow property to True, then when you add a new row to the business object, the column values will be left at DBNull.Value.
By Peter Jones - 2/26/2007

Hi Ben,

Thanks, I had seen that property and read about it but it slipped my mind. Can you please respond to the second question in my previous post.

Cheers, Peter

By StrataFrame Team - 2/27/2007

Ah, as for the second question, the business object does not test the values for NULLs when saving... it just saves whatever is within the DataTable.  The configuration within the BOMapper specifies how the strong-typed property for the field is created.  So, if you use the Return Alternat on Null, then when the DataTable contains a NULL value for the field, the property will return an alternate value.  As for the Set Null on Alternate, when you try to set the alternate value back to the field, the property sets the field to a NULL value rather than the value that you passed.

So, the BOMapper settings only affect the getting and setting of properties.