StrataFrame Forum

Manually creating PK on add running into NULL on new button press in ToolStrip

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

By Gerard Torres - 2/7/2011

I'm working in WinForms in a maintenance form and with a MaintenanceFormToolStrip I'm trying to implement an 'Add' functionality to create a record in a table. I am encountering the following issue and would like to know if StrataFrame includes tools for handling such a case, or if not, a way it might be handled.

Conversion from type 'DBNull' to type 'String' is not valid.

Here's what's happening:
Our form needs the ability to Add/Edit a table for which a non-integer primary key, which is not automatically generated, must be supplied on add. On read/edit, the key can be viewed but not changed.

I have created a StrataFrame TextBox and bound the field to it using the BusinessObject and BindingField properties of the TextBox, like I have done for the rest of the fields using the BO. However, when I click the Add button in the tool strip, I receive the error. Normally there should not be this problem on an Add because the primary key is usually automatically generated, but in this case it must be supplied manually by entering it in the TextBox. I'm guessing that the error is occurring because the BO is attempting to locate a record by the PK bound to the TextBox, but it doesn't exist yet. I mostly get this and why it occurs (to be sure not to add a dup PK?), but need a way around it.

I've tried not binding the TextBox to the BO and that works as I'd expect by not erroring, but then I lose all my automatic handling of validation and I still have to make sure it gets added to the BO on Save (correct?). Also I'd need to figure out how to account for differentiating between an Add and Edit type save operation and make sure I'm only using this logic on Add since an Edit won't alter a PK anyway. I'm not liking this and am not sure it will even work at the end so I'm hoping there's a better way using some framework magic. The initial plan is to do all this work in the ToolStrip.ItemClicked event but I'm sure that's not ideal either.

Any insight is appreciated! Thanks.
By Sam Tenney - 2/7/2011

Hi Gerard,

I ran into a similar problem (see http://forum.strataframe.net/Topic29191.aspx) and I never really received a clear answer to my question of why SF does not initialize primary keys, but I did find a work around that might help you.

First in the YourBO_SetDefaultValues method of your business object, initialize the primary key value yourself.  If the primary key is a string, set it to "" or if it is a numeric type, set it to 0.  That method only runs when a new record is added to the BO.  That should get you past the error and allow the user to enter a correct primary key value.

To allow the user to edit the primary key only when adding a new record and prevent them from editing it on existing records, add an event handler to your form which handles the EditingStateChanged event of your BO.  If you don't know how to do this I can help.  The code should look something like this in C# (sorry for the poor formatting):

// Set ReadOnly appropriately for the primary key textbox.

// It should only be editable when adding a new record.

if (e.BusinessObject.EditingState

== MicroFour.StrataFrame.Business.
BusinessEditingState.Adding)

{

if (Yourtextbox.ReadOnly)

{

Yourtextbox.ReadOnly =
false;

}

}

else

{

if (!YourBO.IsEmpty)

{

Yourtextbox.ReadOnly =
true;

}

}

Sam Tenney