TAGS :Viewed: 6 - Published at: a few seconds ago

[ DataGridView not enfocing foreign key constraint ]

I am trying to develop a business application however i am running into some problems.

I have a DBDataSet that is using my database as its source.

I am using VS to automatically generate DataGridView by dragging from the datasource window and onto the form.

I have two tables. Customer and Sales. There is a one-to-one relationship between them with the id column of Sales Referencing id column of Customer.

However whenever i enter a value in id column of Sales that is not in the id Column of Customer the DataGridView doesn't do throw an exception but instead allows the data to be entered.

However when i try to enter a duplicate primary key in the same table the datagridview responds by throwing an exception.

How can i do the same for foreign key constraints.

Answer 1


ForeignKeyConstraint is not created during the import from database. Right click on Sales table in dataset view -> Add -> Relation... Select your primary and foreign key. "Choose what to create" first or second option. Or you can do it in code: this.DBDataSet.Sales.Constraints.Add( new ForeignKeyConstraint(this.DBDataSet.Customer.IdColumn, this.DBDataSet.Sales.IdColumn));