Results 1 to 5 of 5

Thread: Query Regarding Visual Basic.Net

  1. #1
    Join Date
    May 2008
    Posts
    95

    Query Regarding Visual Basic.Net

    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

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Query Regarding Visual Basic.Net

    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.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Query Regarding Visual Basic.Net

    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).

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Query Regarding Visual Basic.Net

    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)

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Query Regarding Visual Basic.Net

    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

Similar Threads

  1. Basic query, using zone alarm
    By ADAHY in forum Software Development
    Replies: 3
    Last Post: 29-10-2009, 03:10 PM
  2. Query Regarding Visual Basic.Net Forms
    By Kusagra in forum Software Development
    Replies: 3
    Last Post: 25-03-2009, 02:16 PM
  3. Is GUI same like Visual Basic ?
    By Caesar in forum Software Development
    Replies: 2
    Last Post: 02-03-2009, 01:32 PM
  4. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 06:56 PM
  5. Visual Basic on LAN
    By djbbenn in forum Software Development
    Replies: 2
    Last Post: 05-08-2008, 02:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,298,596.89920 seconds with 17 queries