Binding and null date value

Posts   
 
    
mds avatar
mds
User
Posts: 33
Joined: 24-Sep-2006
# Posted on: 24-Sep-2006 21:08:23   

Hi I'm evaluating the last version of LLBGen and i'm trying to use it in a VB.NET 2005 project on SS 2005 db. I have a BIG problem creating UI and binding date field with text box:

When i try to set a non valid date in the textbox the focus won't leave it.

I read in other posts that this behavior has when the length of text inserted is greather than maxlength in db, and the solution proposed was to set maxlength property of textbox disappointed ...

I can't find any overrides or event than let me able to catch the error ...

Can someone help me ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 25-Sep-2006 09:43:03   

The point is that the value you insert isn't valid for the field bound to the textbox. Winforms databinding them simply refuses to set the value and refuses to move on.

The textbox' .Text property is a string. You can't put a string into a datetime field, so it will never work. If you want databinding with a datetime field, bind the field to a datepicker.

Frans Bouma | Lead developer LLBLGen Pro
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 25-Sep-2006 11:32:31   

Otis wrote:

The point is that the value you insert isn't valid for the field bound to the textbox. Winforms databinding them simply refuses to set the value and refuses to move on.

The textbox' .Text property is a string. You can't put a string into a datetime field, so it will never work. If you want databinding with a datetime field, bind the field to a datepicker.

It should work if the string is a 'valid' date time string but unfortunately you don't get to choose a Date format at design time for a TextBox. (Maybe the default is US format?)

You may need to manually create a Binding in code between the entity property and the control's Text property and then do some work when the Parse and Format events are fired to parse/set the string to a valid Datetime according to your required format.

See

ms-help://MS.VSCC.2003/MS.MSDNQTR.2005OCT.1033/cpref/html/frlrfsystemwindowsformsbindingclassparsetopic.htm

for an example (its for a Decimal number not a DateTime but the principal is the same)

Cheers Simon

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 25-Sep-2006 12:47:01   

Thanks a million, Simon! I didn't know that simple_smile

Frans Bouma | Lead developer LLBLGen Pro
mds avatar
mds
User
Posts: 33
Joined: 24-Sep-2006
# Posted on: 26-Sep-2006 09:25:31   

simmotech wrote:

Otis wrote:

The point is that the value you insert isn't valid for the field bound to the textbox. Winforms databinding them simply refuses to set the value and refuses to move on.

The textbox' .Text property is a string. You can't put a string into a datetime field, so it will never work. If you want databinding with a datetime field, bind the field to a datepicker.

It should work if the string is a 'valid' date time string but unfortunately you don't get to choose a Date format at design time for a TextBox. (Maybe the default is US format?)

You may need to manually create a Binding in code between the entity property and the control's Text property and then do some work when the Parse and Format events are fired to parse/set the string to a valid Datetime according to your required format.

See

ms-help://MS.VSCC.2003/MS.MSDNQTR.2005OCT.1033/cpref/html/frlrfsystemwindowsformsbindingclassparsetopic.htm

for an example (its for a Decimal number not a DateTime but the principal is the same)

Cheers Simon

It's all OK now ... Otis point me in the right direction and yes, the solution is what Simon wrote. I created an control derived from text box that expose for binding the new property "DateValue" declared as Nullable (Of date), then I handle the Parse/Format events to return the right value when date it's not valid... something like this:




Private Sub _DataBinding_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles _DataBinding.Format
        If e.DesiredType Is GetType(Nullable(Of Date)) Then
            If e.Value Is System.DBNull.Value Then
                e.Value = Nothing
            End If
        End If
    End Sub

Private Sub _DataBinding_Parse(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles _DataBinding.Parse
        If e.DesiredType Is GetType(DateTime) Then
            If Not IsDate(e.Value) Then
                e.Value = Nothing
            End If
        End If
    End Sub

Thanks wink

Matteo