This documentation is archived and is not being maintained.

Validating Data During Row Changes

You can validate data when the row changes by responding to the RowChanging event. Typed dataset classes expose a table-specific RowChanging event. For example, if the typed dataset contains a table called Employees, the Employees data table object raises a EmployeesRowChanging event. This event passes an event argument object that includes strongly typed properties to give you access to the column being changed. Validation can also be performed during the ColumnChanging event, For more information see Validating Data During Column Changes.

To validate when a row changes

  1. Create an event handler for the RowChanging event of a data table. For more information see Writing Event Handlers (Visual Basic) or Events Tutorial (C#).

    If you are working with a typed dataset, you can respond to the table-specific event, such as EmployeesRowChanging or you can respond to the standard RowChanging event. However, both events are raised so performing the same validation in each event is unnecessary.

  2. In the event handler, get changes to the row by extracting individual columns from the Proposed version of rows using syntax such as the following:
    ' Visual Basic
    newFName = CType(e.Row("FirstName", DataRowVersion.Proposed), String)
    // C#
    newFName = (string)(e.Row["FirstName", DataRowVersion.Proposed]);
    Note   If you are working with the table-specific event, the proposed versions of the columns are available as typed properties of the row, and you can access them using syntax such as the following:
    newFName = e.Row.FirstName
  3. Get the existing versions of columns using syntax such as the following:
    ' Visual Basic
    oldFName = CType(e.Row("FirstName", DataRowVersion.Original), String)
    // C#
    oldFName = (string)(e.Row["FirstName", DataRowVersion.Original]);
  4. Perform the validation. To reject the change, throw an exception. To accept the change, you do not need to perform any special logic.

    The following example shows how you can validate data in a row-changing event. The code checks that if a value already exists for the column SecurityCode, the new value cannot be null — that is, it prevents the column value from being deleted. In the event handler, the code makes sure that there is an existing value by using the data row's HasVersion property.

    ' Visual Basic
    Private Sub employeetable_EmployeesRowChanging(ByVal sender As _
    System.Object, ByVal e As dsEmployees.EmployeesRowChangeEvent) _
    Handles employeetable.EmployeesRowChanging
       Dim original As String = ""
       Dim proposed As String = ""
       If e.Row.HasVersion(DataRowVersion.Original) Then
          original = _
             CType(e.Row("SecurityCode", DataRowVersion.Original), String)
          original = ""
       End If
       proposed = e.Row.SecurityCode
       If original <> "" Then
          If proposed = "" Then
             Throw (New Exception("Security Code cannot be blank"))
          End If
       End If
    End Sub
    // C#
    private void employeetable_EmployeesRowChanging(object sender, dsEmployees.EmployeesRowChangeEvent e) 
       string original = "";
       string proposed = "";
       if (e.Row.HasVersion(DataRowVersion.Original))
          original = 
          (string)(e.Row["SecurityCode", DataRowVersion.Original]);
          original = "";
       proposed = e.Row.SecurityCode;
       if (original != "") 
          if (proposed == "") 
             throw (new System.Exception("Security Code cannot be blank"));

See Also

Dataset Updates in Visual Studio .NET | Introduction to Validating User Input in Web Forms | Validating Data During Column Changes | DataTable.ColumnChanging Event | DataTable.RowChanging Event | DataColumn Properties | Data Validation in Datasets | Writing Event Handlers (Visual Basic) | Events Tutorial (C#)