I am having a query regarding Visual basic.net. I am writing a small vb.net code and i am facing difficulty in declaring a constraint. I just wanted to know that how to declare a constraint on a button.
Please provide some help regarding this
I am having a query regarding Visual basic.net. I am writing a small vb.net code and i am facing difficulty in declaring a constraint. I just wanted to know that how to declare a constraint on a button.
Please provide some help regarding this
You can use constraints to enforce restrictions on the data in a DataTable, in order to maintain the integrity of the data. A constraint is an automatic rule, applied to a column or related columns, that determines the course of action when the value of a row is somehow altered. Constraints are enforced when the System.Data.DataSet.EnforceConstraints property of the DataSet is true. For a code example that demonstrates how to set the EnforceConstraints property, see the EnforceConstraints reference topic.
There are two kinds of constraints in ADO.NET: the ForeignKeyConstraint and the UniqueConstraint. By default, both constraints are created automatically when you create a relationship between two or more tables by adding a DataRelation to the DataSet. However, you can disable this behavior by specifying createConstraints = false when creating the relation.
A ForeignKeyConstraint enforces rules about how updates and deletes to related tables are propagated. For example, if a value in a row of one table is updated or deleted, and that same value is also used in one or more related tables, a ForeignKeyConstraint determines what happens in the related tables.
The DeleteRule and UpdateRule properties of the ForeignKeyConstraint define the action to be taken when the user attempts to delete or update a row in a related table. The following table describes the different settings available for the DeleteRule and UpdateRule properties of the ForeignKeyConstraint.
A ForeignKeyConstraint can restrict, as well as propagate, changes to related columns. Depending on the properties set for the ForeignKeyConstraint of a column, if the EnforceConstraints property of the DataSet is true, performing certain operations on the parent row will result in an exception. For example, if the DeleteRule property of the ForeignKeyConstraint is None, a parent row cannot be deleted if it has any child rows.
You can create a foreign key constraint between single columns or between an array of columns by using the ForeignKeyConstraint constructor. Pass the resulting ForeignKeyConstraint object to the Add method of the table's Constraints property, which is a ConstraintCollection. You can also pass constructor arguments to several overloads of the Add method of a ConstraintCollection to create a ForeignKeyConstraint.
When creating a ForeignKeyConstraint, you can pass the DeleteRule and UpdateRule values to the constructor as arguments, or you can set them as properties as in the following example (where the DeleteRule value is set to None).
Code:Dim custOrderFK As ForeignKeyConstraint = New ForeignKeyConstraint("CustOrderFK", _ custDS.Tables("CustTable").Columns("CustomerID"), _ custDS.Tables("OrdersTable").Columns("CustomerID")) custOrderFK.DeleteRule = Rule.None ' Cannot delete a customer value that has associated existing orders. custDS.Tables("OrdersTable").Constraints.Add(custOrderFK)
Code:Private Sub CreateConstraint(dataSet As DataSet, _ table1 As String, table2 As String, _ column1 As String, column2 As String) ' Declare parent column and child column variables. Dim parentColumn As DataColumn Dim childColumn As DataColumn Dim foreignKeyConstraint As ForeignKeyConstraint ' Set parent and child column variables. parentColumn = dataSet.Tables(table1).Columns(column1) childColumn = dataSet.Tables(table2).Columns(column2) foreignKeyConstraint = New ForeignKeyConstraint _ ("SupplierForeignKeyConstraint", parentColumn, childColumn) ' Set null values when a value is deleted. foreignKeyConstraint.DeleteRule = Rule.SetNull foreignKeyConstraint.UpdateRule = Rule.Cascade foreignKeyConstraint.AcceptRejectRule = AcceptRejectRule.None ' Add the constraint, and set EnforceConstraints to true. dataSet.Tables(table1).Constraints.Add(foreignKeyConstraint) dataSet.EnforceConstraints = True End Sub
Bookmarks