Data-Controls Help

kurf

Active member
Joined
Sep 20, 2004
Messages
33
Location
CT
I have created a database with three tables:
1) Users
2) Empoyees
3) History

The history table is related to the employees table through the EmployeeID column in each table, the EmployeeID is the Primary key in the Employees Table, and EmployeeId is the FKey in history table...

The employees table has an fkey column called userid which is a relation the the users table pkey userid

now I in the datasources window I set the UserTable to be a details view and the othe two tables are gridviews. Then I dragged The USerTable to my form and it created the needed components then i dragged the employees table that was under the usertable and the historytable under the employeestable so the realationships were set right!
now when i run the app i have added 3 records and saved them to the database just fine so when i reload the app the records show up properly. i had to add this code to the bindingnavigators save menuItem click event:
Code:
this.Validate();
            this.userTableBindingSource.EndEdit();
            this.employeeTableTableAdapter.Update(this.myInformationDataSet.EmployeeTable);
            this.historyTableTableAdapter.Update(this.myInformationDataSet.HistoryTable);
            this.userTableTableAdapter.Update(this.myInformationDataSet.UserTable);

now when i click the delete menuItem of the bindingnav control it deletes the row and the related rows from the related tables as desired but if i click save menu item it throws an exception::

The DELETE statement conflicted with the REFERENCE constraint "FK_EmployeeTable_UserTable". The conflict occurred in database "C:\DOCUMENTS AND SETTINGS\RHONDA\MY DOCUMENTS\VISUAL STUDIO 2005\PROJECTS\WAID\WAID\BIN\DEBUG\MYDATA\MYINFORMATION.MDF", table "dbo.EmployeeTable", column UserID.
The statement has been terminated.

How do I commit the changes to the underlying datasource. I dont understand it, cause when I add a new record then enter all the data into the tables and click saveMenuItem it saves fine but not if i delete a record then save it??? Please help! TIA
 
Oh and Im using C# 2.0 Express edition btw sorry for not mentioning that...
 
The problem appears to be due to the fact that you have a Foreign Key constraint defined. If you have a key constraint defined you can not delete any rows from the table with the primary key before you deleting rows from the table with the foreign key (that are currently involved in a relationship with the primary key table). That is, any and all dependencies must be deleted first.
 
Mister E said:
The problem appears to be due to the fact that you have a Foreign Key constraint defined. If you have a key constraint defined you can not delete any rows from the table with the primary key before you deleting rows from the table with the foreign key (that are currently involved in a relationship with the primary key table). That is, any and all dependencies must be deleted first.

So this should do the trick:

Code:
this.historyTableBindingSource.RemoveCurrent();
            this.historyTableTableAdapter.Update(this.myInformationDataSet.HistoryTable);
            this.employeeTableBindingSource.RemoveCurrent();
            this.employeeTableTableAdapter.Update(this.myInformationDataSet.EmployeeTable);
            this.userTableBindingSource.RemoveCurrent();
            this.userTableTableAdapter.Update(this.myInformationDataSet.UserTable);

I have placed this in a buttons click event and it works perfect
but how would i implement this using the bindingnavigator control
do I need to override the delete buttons click event!
 
Back
Top