Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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 don't 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

Posted
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.
Posted
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' date=' any and all dependencies must be deleted first.[/quote']

 

So this should do the trick:

 

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 button's 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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...