473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Saving Data from Related Data Tables (Hierarchical Update)

2
Hi Everyone,

I am new to C#, kind of new to programming altogether. It is something I am really enjoy learning but I am a little stuck with hierarchical updates.

I am using Visual Studio 2008 and SQL Express.

I have been working on a simple application for my girlfriends business. I have a database with 2 tables, Customers & Vacancies. I use the IDE to create the data-bound controls, Customers being "Details" and Vacancies being "DataGridView".

On the Customer table I have a CustomerID column. This is a Primary Key and has Identity configured so each record is unique and the number is incremented.

I created a Foreign Key Constraint (one to many) from Customer - CustomerID to Vacancies - CustomerID. I did this so each record in the Vacanacie table would update when the Customer is changed on the form.

I may have got some words wrong there, but hopefully you know what I am getting at =)

When I run the program, i receive the following error in debug mode when I try and add a user and vacancy at the same time.

"The UPDATE statement conflicted with the FOREIGN KEY constraint"

I found an article on MSDN (http://msdn.microsoft.com/en-us/library/bb384432.aspx) that sounds like just what I am trying to achieve.

I went through the walkthrough step by step, when I run the program in debug and try to add a customer and an order together, I receive the following error

"The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Orders_Employees". The conflict occurred in database "90C91394EA995F36B4DCCD90A88CCCD8_008\PROJECTS\HIE RARCHICALUPDATEWALKTHROUGH\HIERARCHICALUPDATEWALKT HROUGH\BIN\DEBUG\NORTHWND.MDF", table "dbo.Employees", column 'EmployeeID'."

This is the same error as I receive in my application.

Has anyone else had problems with this walkthrough? If I can solve this, then I am confident I can solve the problem with my application.

I hope all this makes sense, this is a huge learning curve for me.

Many thanks

Dave
Mar 4 '10 #1
4 3709
jkmyoung
2,057 Expert 2GB
It says that the error stems from the "FOREIGN KEY constraint "FK_Orders_Employees", table "dbo.Employees", column 'EmployeeID'."


What does this foreign key represent?
Or have you just renamed everything in your example?
Mar 4 '10 #2
Meddix
2
Hi jkmyoung,

Thanks for replying.

The error "FOREIGN KEY constraint "FK_Orders_Employees", table "dbo.Employees", column 'EmployeeID'." I received when I did the example on the MSDN site.

I am receiving the same error in my application, which is the CustomerID one. Maybe I should have left that out, probably made a bit more confusing for other readers.

Because I am receiving the same error in the MSDN walkthrough, I thought it would be easier to give that as an example for people to look at and even do and see if they receive the same problem as me or even resolve it!

If I can find a resolution to the MSDN walkthrough, then I should be able to use the same resolution to fix my own app.

Hope that makes a little more sense =)

Many thanks
Mar 5 '10 #3
jkmyoung
2,057 Expert 2GB
ah, ok. Can you post your udpate statement, and the error you are getting?
Mar 5 '10 #4
Hi there,

In your scenario, the VS 2008 autogenerated code won't work unless you do further configuration. Please follow these steps:

1. Right-click on your data set listed in DataSources and go to Edit DataSet with Designer,

2. Once the designer opens, select the parent table adapter, right-click on it and go to Configure,

3. Table adapter configuration wizard opens, click on the Advanced Options button and make sure that the 'Refresh the data table' option is checked. By default it is checked, yet make sure anyway. Close the wizard and go back to the dataset designer. Now double-click the table relation,

4. Relation dialogbox opens, here select 'Both Relation and Foreign Key Constraint' and below choose Cascade for both Update Rule and Delete Rule. Click OK button,

5. Go to the place in your code where the table adapters are filled (where the Fill method is called). If your code is autogenerated, these methods are called in Form_Load handler. Make sure that the order of calls is correct. The correct order is to call Fill for parent table adapter and then for the child table adapter. It might be the other way round, so correct it manually.

Run and test your code. It should work as expected now.

Lukasz
May 16 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Oldhandandy | last post by:
I have a datagrid linked to a SQL Stored Procedure, the data is extracted from 4 tables. Data is only updated not inserted and the update is to only 1 of the tables, the other three tables are...
0
by: Scanner2001 | last post by:
I am researching some options for loading hierarchically related data from xml into an sql server database. I do not have the primary keys in the source xml files. The primary keys are identity...
8
by: Raven | last post by:
Hello, I get a error message when I try to Add data in a relational Access Database. This is the error message: "You cannot add or change a record because a related record is required in...
4
by: Daisy | last post by:
Let's say I've got a forum, where users can be moderators of each forum. Tables look like this: USER -------- user_key name FORUM
2
by: Trillium | last post by:
I have an application with a list of questions, which are associated with answers by a questId in a one-to-one relationship. The questions may come from an XML file or a SQL Server database. The...
2
by: Bob | last post by:
I got three related datagrid views one parent and two children of the same. The two child tables contain many thousands of records and some of the contents are bitmap files in a sql server...
7
by: Pradeep | last post by:
Hello, I need to take a set of input tables and create an XML output file. The format of the XML output must be user-definable and must be intuitive enough for non-techies to use. input...
1
by: Magnus | last post by:
I'm testing walkthrough saving data to a Database (Multiple Tables). http://msdn2.microsoft.com/en-us/library/4esb49b4(VS.80).aspx In the famous Customer/Order example, I'm getting referential...
1
by: nmrpa91290 | last post by:
I have a vb 2005 program that I am writing. It is just a form with a datagridview, textbox, and import command button. The code looks like this: Private Sub Form1_Load(ByVal sender As...
7
by: erikcw | last post by:
Hi, I'm working on a web application where each user will be creating several "projects" in there account, each with 1,000-50,000 objects. Each object will consist of a unique name, an id, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.