Using a System.Int32 and null values


Author
Message
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Instead of overriding the KeyUp, inherit the SF textbox and override the ProcessCmdKey method.  This will give you 100% control on the input before the value is actually changed.  You can also override the Text property (or whichever property to which you are binding) and provide additional logic in the Set/Get which will then give you 100% control on the input as well as the values coming and going from the control.  If you replace the logic in these two locations that should be everyplace that you need in order to change the underlying logic to meet your needs.
Lesscher
Lesscher
StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)
Group: StrataFrame Users
Posts: 9, Visits: 19
Unfortunately, the approach above doesn't completely work for us. The underlying value is set to null when the textbox is emptied. This is the behavior we want.



However, we let StrataFrame create a FieldPropertyChanged event for us en we added some logic to this event. This event is not fired because we are not changing the field, but the underlaying value in the datatable/datarow. But we do want to raise the event!



I've tried 2 ways to get this to work:

1. I have tried to raise the FieldProperyChanged event from the OnKeyUp but don't know how to do it (if it is at all possible).

2. I have tried to set the field in another way than through the CurrentRow property, but I can't find a way to do this.



Can anyone help out?
Lesscher
Lesscher
StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)
Group: StrataFrame Users
Posts: 9, Visits: 19
We found a simple solution to our problem. We overrided the standard SF textbox with only the following overrided event:


/// <summary>
/// Overrides the KeyUp event.
/// </summary>
/// <param name="e">A System.Windows.Forms.KeyEventArgs that contains the event data.</param>
protected override void OnKeyUp(KeyEventArgs e)
{
if (this.Text.Length==0)
{
((
BusinessLayer)this.BusinessObject).CurrentRow[this.BindingField] = DBNull.Value;
}

base.OnKeyUp(e);

}

In the datamapper we just say for all of our integer fields that they are nullable generics (no replacement values anymore :w00tSmile

Any remarks about this approach?

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
You might try using Integer.MaxValue or Integer.MinValue, as I'm guessing that it is unlikely these would be valid values. You'd just Type that (or System.Int32.MaxValue/MinValue for C# I guess) as the replacement value.
Lesscher
Lesscher
StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)
Group: StrataFrame Users
Posts: 9, Visits: 19
I played a little with subclassing (thanks Greg for your sample) and I think I understand it. Smile
However, I still don’t get what I want exactly: 

I have a field say ‘NrOfOccurences’: in the UI, the user can fill in a negative number or positive number or leave the field empty (if he does not know the value). When the field is empty I want a null value in the database. I have several of these fields. 

I understand SF always needs a replacement value when putting a null value in the database (when using INT values)?

I cannot let the user put a -1 (or 0) in the control (in order to put a null value in the database) because these values could be real values. 

Are there any other ways to handle this issue?

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Lesscher,



Another approach would be to create a subclassed text box that alters the Text property and handles the Text property being empty when setting the underlying value. You'd leave the BO setup to return an alternate value for Null/Save Null on that alternate value (zero in this case). This makes any programmatic access to the data easier, as integers don't have NULL as a possibility.



I did this for the case of identities. If the ID is <0 (i.e. a new value), it displays "(new)", otherwise it displays the actual ID.



I overwrote the Text property. The setter just sets the Text property on the base class. The getter tests the value of the base class and simply displays something different if the number is negative.



I'd think you'd need to add some code to the setter, to set a "0" to the base class if the text is blank. And in the getter, you'd test for zero, displaying a blank in that case, otherwise whatever they have entered.



Hope that helps!



Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Well, if you are using a textbox for integer values, then you would take the same approach.  Generally I never allow an empty value to be entered through the UI for an integer value.  An integer field should always have a value within the control...if you are allowing an empty value for 0 in a textbox, then you may have to customize then step into the Set of the property to verify that the underlying value is 0.
Lesscher
Lesscher
StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)
Group: StrataFrame Users
Posts: 9, Visits: 19
Trent L. Taylor (08/19/2008)
You have not read through all of the posts or docs then Wink

You will want to use a Return / Set option depending on whether it is a reference or value type.  But this is how you will get what you are looking for.

Hi Trent, I tried this with the value 0 and yes, a NULL value is saved in the database when the user enters a 0.

What I want is that the user enters nothing at all in the textbox (and also, I don't want to see a 0 when there is a NULL value in the database, just an empty textbox). With strings this is working fine, but I cannot get this to work with integers...

Or am I still missing something... Hehe

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
You have not read through all of the posts or docs then Wink

You will want to use a Return / Set option depending on whether it is a reference or value type.  But this is how you will get what you are looking for.

Lesscher
Lesscher
StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)StrataFrame Beginner (9 reputation)
Group: StrataFrame Users
Posts: 9, Visits: 19
Hi all,

I have several fields in my table of type System.Int32 and they are allowed to be null. I want to put a NULL value (not a 0) in the database table when the user empties a textbox (bound to the BO).

I already read the posts about this solution using String.Empty but thats not working for me... Also, I tried to use nullable generics in the datamapper but then, when the user empties the textbox, the new (empty) value isn't saved to the BO (the old value remains)

This can't be that difficult, am I right?

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search