Validating Data Using a Error Provider

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan

    #1

    Validating Data Using a Error Provider

    A binding navigator control adds the following code for when the Save button is clicked:
    Me.Validate()
    Me.UserBindingS ource.EndEdit()
    Me.UserTableAda pter.Update(Me. UserDataSet.Use r)"

    You can add code to the column changing event for the dataset by using the dataset designer, for example:

    Private Sub UserDataTable_C olumnChanging(B yVal sender As System.Object, ByVal e As System.Data.Dat aColumnChangeEv entArgs) Handles Me.ColumnChangi ng
    If (e.Column.Colum nName = Me.WindowsLogin Column.ColumnNa me) Then
    If CType(e.Propose dValue, String) = "" Then
    e.Row.SetColumn Error(e.Column, "Cannot be blank")
    Else
    e.Row.SetColumn Error(e.Column, "")
    End If
    End If

    The problem is, the validation is not actually done until the .EndEdit event is fired. Me.Validate can be overriden for form validation (not validation on the dataset columns). So EndEdit throws a runtime error say if you have Null for a column that does not allow Nulls. I'm having a very hard time getting my validation to work using the error provider rather than throwing runtime errors. Help!

  • Ryan

    #2
    Re: Validating Data Using a Error Provider

    Ok I'm just going to use form validation and column validation I guess (seems silly). How do I return false since the controls_valida ting is a sub and not a function?
    "Ryan" <Tyveil@newsgro ups.nospamwrote in message news:O%23IuTZHo GHA.4176@TK2MSF TNGP05.phx.gbl. ..
    A binding navigator control adds the following code for when the Save button is clicked:
    Me.Validate()
    Me.UserBindingS ource.EndEdit()
    Me.UserTableAda pter.Update(Me. UserDataSet.Use r)"

    You can add code to the column changing event for the dataset by using the dataset designer, for example:

    Private Sub UserDataTable_C olumnChanging(B yVal sender As System.Object, ByVal e As System.Data.Dat aColumnChangeEv entArgs) Handles Me.ColumnChangi ng
    If (e.Column.Colum nName = Me.WindowsLogin Column.ColumnNa me) Then
    If CType(e.Propose dValue, String) = "" Then
    e.Row.SetColumn Error(e.Column, "Cannot be blank")
    Else
    e.Row.SetColumn Error(e.Column, "")
    End If
    End If

    The problem is, the validation is not actually done until the .EndEdit event is fired. Me.Validate can be overriden for form validation (not validation on the dataset columns). So EndEdit throws a runtime error say if you have Null for a column that does not allow Nulls. I'm having a very hard time getting my validation to work using the error provider rather than throwing runtime errors. Help!

    Comment

    • Walter Wang [MSFT]

      #3
      Re: Validating Data Using a Error Provider

      Hi Ryan,

      Thank you for your post.

      Based on my understanding, your question is how to use ErrorProvider to
      display errors when working with a DataSet. If I've misunderstood anything,
      please feel free to post here.

      First, please take a look at following MSDN Library article:

      #Walkthrough: Adding Validation to a Dataset
      http://msdn2.microsoft.com/en-us/library/ms171930.aspx

      From this example, we will see that the ErrorProvider is working when you
      move focus away from the field. This will give the user a visual clue of
      which field is filled wrong value. Also, the validation will only work when
      you changed the field's value.

      Hope this helps. Please feel free to post here if anything is unclear.

      Regards,
      Walter Wang (wawang@online. microsoft.com, remove 'online.')
      Microsoft Online Community Support

      =============== =============== =============== =====
      When responding to posts, please "Reply to Group" via your newsreader so
      that others may learn and benefit from your issue.
      =============== =============== =============== =====

      This posting is provided "AS IS" with no warranties, and confers no rights.

      Comment

      • Ryan

        #4
        Re: Validating Data Using a Error Provider

        Hi Walter,

        I actually used that walkthrough prior to this post. There are 2 problems
        with it.
        1) If the user put's nothing in the field (leaves it blank), the
        ErrorProvider does not appear. If using the ErrorProvider control during
        the control's (text box for example) validating event rather than in the
        dataset, it works whether the user enters anything or not, it validates when
        the control loses focus whereas the dataset column_changing validating event
        only occurs if the field has changed.
        2) Just because the error provider error is set (using seterror()), it still
        does not stop the database update from happening. So if the field contains
        something that violates the dataset integrity a runtime error will occur.
        Perhaps a try-catch block that does nothing in the catch block is the way to
        go (since the ErrorProvider handles informing the user of the input error).

        Ryan

        "Walter Wang [MSFT]" <wawang@online. microsoft.comwr ote in message
        news:CivL0XPoGH A.4260@TK2MSFTN GXA01.phx.gbl.. .
        Hi Ryan,
        >
        Thank you for your post.
        >
        Based on my understanding, your question is how to use ErrorProvider to
        display errors when working with a DataSet. If I've misunderstood
        anything,
        please feel free to post here.
        >
        First, please take a look at following MSDN Library article:
        >
        #Walkthrough: Adding Validation to a Dataset
        http://msdn2.microsoft.com/en-us/library/ms171930.aspx
        >
        From this example, we will see that the ErrorProvider is working when you
        move focus away from the field. This will give the user a visual clue of
        which field is filled wrong value. Also, the validation will only work
        when
        you changed the field's value.
        >
        Hope this helps. Please feel free to post here if anything is unclear.
        >
        Regards,
        Walter Wang (wawang@online. microsoft.com, remove 'online.')
        Microsoft Online Community Support
        >
        =============== =============== =============== =====
        When responding to posts, please "Reply to Group" via your newsreader so
        that others may learn and benefit from your issue.
        =============== =============== =============== =====
        >
        This posting is provided "AS IS" with no warranties, and confers no
        rights.
        >

        Comment

        • Ryan

          #5
          Re: Validating Data Using a Error Provider

          Hmmm I tried the try-catch block but it seems the error occurs before the
          DataTable_Colum nChanging event occurs (where the validation takes place).
          So if a field violates database integrity, nothing happens.

          "Ryan" <Tyveil@newsgro ups.nospamwrote in message
          news:%23uocl3Po GHA.4688@TK2MSF TNGP03.phx.gbl. ..
          Hi Walter,
          >
          I actually used that walkthrough prior to this post. There are 2 problems
          with it.
          1) If the user put's nothing in the field (leaves it blank), the
          ErrorProvider does not appear. If using the ErrorProvider control during
          the control's (text box for example) validating event rather than in the
          dataset, it works whether the user enters anything or not, it validates
          when the control loses focus whereas the dataset column_changing
          validating event only occurs if the field has changed.
          2) Just because the error provider error is set (using seterror()), it
          still does not stop the database update from happening. So if the field
          contains something that violates the dataset integrity a runtime error
          will occur. Perhaps a try-catch block that does nothing in the catch block
          is the way to go (since the ErrorProvider handles informing the user of
          the input error).
          >
          Ryan
          >
          "Walter Wang [MSFT]" <wawang@online. microsoft.comwr ote in message
          news:CivL0XPoGH A.4260@TK2MSFTN GXA01.phx.gbl.. .
          >Hi Ryan,
          >>
          >Thank you for your post.
          >>
          >Based on my understanding, your question is how to use ErrorProvider to
          >display errors when working with a DataSet. If I've misunderstood
          >anything,
          >please feel free to post here.
          >>
          >First, please take a look at following MSDN Library article:
          >>
          >#Walkthrough : Adding Validation to a Dataset
          >http://msdn2.microsoft.com/en-us/library/ms171930.aspx
          >>
          >From this example, we will see that the ErrorProvider is working when you
          >move focus away from the field. This will give the user a visual clue of
          >which field is filled wrong value. Also, the validation will only work
          >when
          >you changed the field's value.
          >>
          >Hope this helps. Please feel free to post here if anything is unclear.
          >>
          >Regards,
          >Walter Wang (wawang@online. microsoft.com, remove 'online.')
          >Microsoft Online Community Support
          >>
          >============== =============== =============== ======
          >When responding to posts, please "Reply to Group" via your newsreader so
          >that others may learn and benefit from your issue.
          >============== =============== =============== ======
          >>
          >This posting is provided "AS IS" with no warranties, and confers no
          >rights.
          >>
          >
          >

          Comment

          • Walter Wang [MSFT]

            #6
            Re: Validating Data Using a Error Provider

            Hi Ryan,

            Thank you for your post.

            Using the DataTable's ColumnChanging event, it must occur after you changed
            the data, so for a default empty field, this event will not be triggered,
            thus the ErrorProvider didn't appear. I recommend you take a look at
            following article:

            #Extending Windows Forms with a Custom Validation Component Library
            http://msdn.microsoft.com/library/de...us/dnforms/htm
            l/winforms0316200 4.asp

            With this custom validation component library, you can use these validators
            and check each validator's IsValid property after you called "Validate"
            method in the save button's click event. If any validator is invalid, don't
            call remaining "EndEdit" and "TableAdapter.U pdate".

            Hope this helps. Please feel free to post here if anything is unclear.


            Regards,
            Walter Wang (wawang@online. microsoft.com, remove 'online.')
            Microsoft Online Community Support

            =============== =============== =============== =====
            When responding to posts, please "Reply to Group" via your newsreader so
            that others may learn and benefit from your issue.
            =============== =============== =============== =====

            This posting is provided "AS IS" with no warranties, and confers no rights.

            Comment

            Working...