asp.net.ph

DataSet.EnforceConstraints Property

System.Data Namespace   DataSet Class


Sets or retrieves a value indicating whether constraint rules are followed when attempting any update operation.

Syntax


Script DataSet.EnforceConstraints [ = true | false ]

Property Value

This property accepts or returns only a boolean value: true, if rules are enforced; otherwise, false.

The property is read/write with a default value of true.

Exceptions


Exception Type Condition
ConstraintException Occurs when One or more constraints cannot be enforced.

Remarks

See the Constraints property for more details.

Example

The following example initializes a DataSet with one table, one column, five rows, and one UniqueConstraint. The EnforceConstraints property is set to false and the values of each row are set to the same value. When the EnforceConstraints property is reset to true, a ConstraintException is generated.

private void DemonstrateEnforceConstraints ( ) {
   // create a DataSet with one table, one column and a UniqueConstraint.
   DataSet ds = new DataSet ( "myDataSet" );
   DataTable t = new DataTable ( "myTable" );
   DataColumn c = new DataColumn ( "col1" );
   // a UniqueConstraint is added when the Unique property is true.
   c.Unique = true;
   t.Columns.Add ( c );
   ds.Tables.Add ( t );
   Response.Write ( "constraints.count: " + t.Constraints.Count );
   // add five rows.
   DataRow r ;
   for ( int i=0; i < 5; i++ ) {
      r = t.NewRow ( );
      r [ "col1" ] = i;
      t.Rows.Add ( r );
  }
   t.AcceptChanges ( );

   ds.EnforceConstraints = false;
   // change the values of all rows to 1.
   foreach ( DataRow thisRow in t.Rows ) {
      thisRow [ "col1" ] = 1;
      // Response.Write ( "<br>" + thisRow [ 0 ] );
  }
   try {
      ds.EnforceConstraints = true;
  }
   catch ( System.Data.ConstraintException ex ) {
      Response.Write ( "ConstraintException: " + ex.Message );
  }
   catch ( System.Exception sysEx ) {
      Response.Write ( sysEx.GetType ( ).Name );
      Response.Write ( sysEx.Message );
   }
}
  C# VB

See Also

DataSet Members   Constraints   ConstraintException   ForeignKeyConstraint   DataTable   UniqueConstraint Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph