Home Dashboard Directory Help

Problem with simple DataBinding and Validation with Cancel=true still updates DataSource by TrickAbort


Status: 

Closed
 as Won't Fix Help for as Won't Fix


8
0
Sign in
to vote
Type: Bug
ID: 490267
Opened: 9/16/2009 3:15:58 PM
Access Restriction: Public
1
Workaround(s)
view
2
User(s) can reproduce this bug

Description

A TextBox Control has a simple DataBinding. When the TextBox Validating Event detects an error, sets e.Cancel=True and exits, the DataSource is incorrectly updated with the invalid data.

Demo program...
1) creates a simple Bind between a TextBox and a test object.
2) defines a Validating event on the TextBox
3) Validating event detects the blank field and sets e.Cancel=true.

Sample Code:

Public Class Form1
    Private _anObject As New aClass

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TextBox1.DataBindings.Add(New Binding("Text", _anObject, "myProperty"))
    End Sub

    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        Debug.Print("Validating: ctrl:'{0}' obj:'{1}", TextBox1.Text, _anObject.myProperty)
        If TextBox1.Text = "" Then
            Debug.Print("Set Cancel=true; should not update bound object")
            e.Cancel = True
        End If
    End Sub
End Class

Public Class aClass
    Private _myProperty As String = "aaaa"
    Public Property myProperty() As String
        Get
            Return _myProperty
        End Get
        Set(ByVal value As String)
            Debug.Print("Updating: _myProperty:'{0}' value:'{1}'", _myProperty, value)
            _myProperty = value
        End Set
    End Property
End Class
Details
Sign in to post a comment.
Posted by lanwake on 1/7/2014 at 2:07 PM
It is completely ridiculous that this bug has not yet been fixed (2014)!
Just lost 3 hours trying to debug my code because of this.
Posted by Microsoft on 9/20/2009 at 6:50 PM
Thanks for reporting the issue.

Customer feedback is a critical part of a successful, impactful product release. Unfortunately another part is the reality of schedules and the need to get the bits into production. We have evaluated the issue that you reported and it does not meet the criteria to be addressed in this release. This evaluation is carefully done and considers many aspects including fix cost, breaking changes, globalization, performance, etc.

Many customers have found it useful to discuss issues like this in the forums (http://forums.microsoft.com/msdn/default.aspx?siteid=1) where Microsoft and other members of the community can suggest workarounds.

Before we even begin work on the next release of the NET Framework release where we can make changes like this one we will devote time to addressing the top issues/suggestion (in terms of number of votes, lack of workarounds, etc). So please do continue to vote for this item if it is causing issues.

Thanks,
UIFx Team
Posted by Microsoft on 9/18/2009 at 12:33 AM
Thanks for your feedback. We are routing this bug to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.

Thank you,
Visual Studio Product Team
Sign in to post a workaround.
Posted by monikermonoman on 11/2/2009 at 7:16 AM
Basically, setting e.Cancel = True does not prevent the internal parsing event and data binding happening.
It does however prevent the Control.Validation event from firing, so even though the data source has been updated with incorrect data, you can prevent the user from proceeding until the data is correct.

To actually prevent the data source from updating, you need to validate in the Binding.Parse event and throw an exception if the data is not valid. The message of your exception will be sent through to an ErrorProvider that is bound to the binding source.