Export (0) Print
Expand All

ConstraintCollection.CanRemove Method

Indicates whether a Constraint can be removed.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

'Declaration
Public Function CanRemove ( _
	constraint As Constraint _
) As Boolean

Parameters

constraint
Type: System.Data.Constraint

The Constraint to be tested for removal from the collection.

Return Value

Type: System.Boolean
true if the Constraint can be removed from collection; otherwise, false.

The default behavior whenever a DataRelation is added to a DataSet, is to add a ForeignKeyConstraint to the parent table and a UniqueConstraint to the child table. The UniqueConstraint is applied to the primary key column of the parent table, and the ForeignKeyConstraint is applied to the foreign key column of the child table. Because trying to remove the UniqueConstraint before removing the ForeignKeyConstraint causes an exception to be thrown, you should always use the CanRemove method before calling Remove, to make sure that the UniqueConstraint can be removed.

The following example uses the CanRemove method to determine whether a Constraint can be removed, before trying to remove it.

Private Sub TryRemove(dataSet As DataSet)
    Try 
        Dim customersTable As DataTable = dataSet.Tables("Customers")
        Dim constraint As Constraint = customersTable.Constraints(0)
        Console.WriteLine("Can remove? " & _
            customersTable.Constraints.CanRemove(constraint).ToString())

    Catch ex As Exception
        ' Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", _
            ex.GetType().ToString())
    End Try 
End Sub

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft