Results 1 to 10 of 10

Thread: [Resolved] Problem with data-bound controls validation

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    38

    Re: Problem with data-bound controls validation

    Doing it manually would probably be better, yes, but that's exactly what I'm avoiding since I have about 50 different fields on that single table so creating them automatically saves a whole lot of work.

    This is what I came up with, probably not the best solution, but it works:
    VB.NET Code:
    1. Private Sub GenericTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs)
    2.         If sender.Text.Length = 0 Then
    3.             sender.CausesValidation = False
    4.             Dim currentRow As DataRowView = TryCast(EnsaiosBindingSource.Current, DataRowView)
    5.             currentRow.BeginEdit()
    6.             currentRow(sender.DataBindings.Item("Text").BindingMemberInfo.BindingField) = System.DBNull.Value
    7.             currentRow.EndEdit()
    8.             sender.CausesValidation = True
    9.         End If
    10.     End Sub

    Thank you for your help Kevin.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Problem with data-bound controls validation

    Quote Originally Posted by mf12 View Post
    Doing it manually would probably be better, yes, but that's exactly what I'm avoiding since I have about 50 different fields on that single table so creating them automatically saves a whole lot of work.

    This is what I came up with, probably not the best solution, but it works:
    VB.NET Code:
    1. Private Sub GenericTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs)
    2.         If sender.Text.Length = 0 Then
    3.             sender.CausesValidation = False
    4.             Dim currentRow As DataRowView = TryCast(EnsaiosBindingSource.Current, DataRowView)
    5.             currentRow.BeginEdit()
    6.             currentRow(sender.DataBindings.Item("Text").BindingMemberInfo.BindingField) = System.DBNull.Value
    7.             currentRow.EndEdit()
    8.             sender.CausesValidation = True
    9.         End If
    10.     End Sub

    Thank you for your help Kevin.
    Your welcome for the help.
    Might I suggest trying to set Option Strict On. Although your code above works it is late binding meaning if there was a possibilty of an issue with the code having Option Strict On gives you a better chance at correcting it prior to running the app.

    So for the code above the following would be fine with Option Strict One
    Code:
    Private Sub GenericTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim CurrentBox = DirectCast(sender, TextBox)
    
        If CurrentBox.Text.Length = 0 Then
            CurrentBox.CausesValidation = False
            Dim currentRow As DataRowView = TryCast(EnsaiosBindingSource.Current, DataRowView)
            currentRow.BeginEdit()
            currentRow(CurrentBox.DataBindings.Item("Text").BindingMemberInfo.BindingField) = System.DBNull.Value
            currentRow.EndEdit()
            CurrentBox.CausesValidation = True
        End If
    End Sub
    My original suggestion would then be (for generic usage)
    Code:
    Private Sub GenericDecimalTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim CurrentBox = DirectCast(sender, TextBox)
    
        If CurrentBox.Text.Equals("") Then
            Dim currentRow As DataRowView = TryCast(bsDecimals.Current, DataRowView)
            Dim FieldName As String = CurrentBox.DataBindings.Item("Text") _
                                      .BindingMemberInfo.BindingField
    
            If chkUseNull.Checked Then
                currentRow.Row(FieldName) = System.DBNull.Value
            Else
                currentRow.Row(FieldName) = 0
            End If
        End If
    
    End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    38

    Re: Problem with data-bound controls validation

    Quote Originally Posted by kevininstructor View Post
    Your welcome for the help.
    Might I suggest trying to set Option Strict On. Although your code above works it is late binding meaning if there was a possibilty of an issue with the code having Option Strict On gives you a better chance at correcting it prior to running the app.

    So for the code above the following would be fine with Option Strict One
    Code:
    Private Sub GenericTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim CurrentBox = DirectCast(sender, TextBox)
    
        If CurrentBox.Text.Length = 0 Then
            CurrentBox.CausesValidation = False
            Dim currentRow As DataRowView = TryCast(EnsaiosBindingSource.Current, DataRowView)
            currentRow.BeginEdit()
            currentRow(CurrentBox.DataBindings.Item("Text").BindingMemberInfo.BindingField) = System.DBNull.Value
            currentRow.EndEdit()
            CurrentBox.CausesValidation = True
        End If
    End Sub
    My original suggestion would then be (for generic usage)
    Code:
    Private Sub GenericDecimalTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim CurrentBox = DirectCast(sender, TextBox)
    
        If CurrentBox.Text.Equals("") Then
            Dim currentRow As DataRowView = TryCast(bsDecimals.Current, DataRowView)
            Dim FieldName As String = CurrentBox.DataBindings.Item("Text") _
                                      .BindingMemberInfo.BindingField
    
            If chkUseNull.Checked Then
                currentRow.Row(FieldName) = System.DBNull.Value
            Else
                currentRow.Row(FieldName) = 0
            End If
        End If
    
    End Sub
    Thanks lol, that was some code I had just written in a rush.

    Today, after doing some more research, I found this:
    http://stackoverflow.com/questions/4...921107#4921107
    http://msdn.microsoft.com/en-us/libr...ndp1_v3_topic5

    This is the most important part of that stackoverflow response:
    Once the data source has been updated (to DBNull), the binding mechanism will attempt to then repopulate the Textbox with the newly updated data source value. When the underlying data source value is DBNull, the value used for the bound control is governed by the Binding class's NullValue property.
    So, as a TextBox's Text property is a String and it gets assigned a null value, an error occurs.
    The solution is to change the BindigClass' NullValue property and set it to a valid value (i.e. String.Empty).

    This is quite easy when we manually add the bindings to a few controls in our code but I wonder if there's a way to do the same with controls and bindings created by the form designer...
    Last edited by mf12; Aug 26th, 2011 at 07:14 AM.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Problem with data-bound controls validation

    Quote Originally Posted by mf12 View Post
    Thanks lol, that was some code I had just written in a rush.

    Today, after doing some more research, I found this:
    http://stackoverflow.com/questions/4...921107#4921107
    http://msdn.microsoft.com/en-us/libr...ndp1_v3_topic5

    This is the most important part of that stackoverflow response:


    So, as a TextBox's Text property is a String and it gets assigned a null value, an error occurs.
    The solution is to change the BindigClass' NullValue property and set it to a valid value (i.e. String.Empty).

    This is quite easy when we manually add the bindings to a few controls in our code but I wonder if there's a way to do the same with controls and bindings created by the form designer...
    In regards to working with the form designer, I honestly have no idea as I never done things this way, only manually. Have you tried using the generic method?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width