ConstraintCollection.CanRemove Method

Indicates whether a Constraint can be removed.

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

public bool CanRemove (
	Constraint constraint
)
public boolean CanRemove (
	Constraint constraint
)
public function CanRemove (
	constraint : Constraint
) : boolean
Not applicable.

Parameters

constraint

The Constraint to be tested for removal from the collection.

Return Value

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 void TryRemove(DataSet dataSet)
{
    try
    {
        DataTable customersTable = dataSet.Tables["Customers"];
        Constraint constraint = customersTable.Constraints[0];
        Console.WriteLine("Can remove? " + 
            customersTable.Constraints.CanRemove(constraint));
    }
    catch(Exception ex) 
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", 
            ex.GetType());
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show: