473,394 Members | 1,701 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,394 software developers and data experts.

MS Walkthrough: Saving Data to a Database (Multiple Tables)

I'm testing walkthrough saving data to a Database (Multiple Tables).

http://msdn2.microsoft.com/en-us/lib...b4(VS.80).aspx

In the famous Customer/Order example, I'm getting referential integrity
problems "The INSERT statement conflicted with the FOREIGN KEY
constraint..." if the identity/primary key column is "out of sync" with the
identity value in the database. I can easily acheive this f.ex. by pressing
add new record, cancel then add again and filling out customer and order
details. Obviously the customerId column in the order table has the wrong
values after the customer table has been inserted, as the customerid in the
customer table is updated to whatever number the database has given to the
record. Should I now update the columns referencing the customerID field
manually by code? Or how is a good way to do this?

I can't find any articles on how this can be effectively handled, can anyone
give me some pointers or give an example on how this should/could be
handled?

- Magnus
Nov 8 '06 #1
1 2581

This is a good reason to use GUID's.

However, I have a INT (identity) work around.
Create your Customers table (DataTable) and tack on a "OrdinalID"
CustomerID,LastName,FirstName,OrdinalID
123,'Smith','John',1
234,'Jones','Mary',2

Create an Orders (DataTable)
OrderID,ProductID,Quanity,OrdinalID
0,1001,3,1
0,1002,2,1
0,1001,8,2
0,1003,5,2

The second table says:
John Smith (OrdinalID=1) ordered a productid of 1001, and wanted 3(qty) of
them.
John Smith (OrdinalID=1) ordered a productid of 1002, and wanted 2(qty) of
them.
Mary Jones (OrdinalID=2) ordered a productid of 1001, and wanted 8(qty) of
them.
Mary Jones (OrdinalID=2) ordered a productid of 1003, and wanted 5(qty) of
them.

Ok...ship that into the stored procedure.

Put everything into @variable or @temp tables.

When you create your Customers, you'll get back the @@IDENTITY, lets say you
get back 12221 as the IDENTITY.

If you did a bulk insert, then that means.

MaryJones was 12221, and JohnSmith was 12220.

Update the @variable or #temp table ....
123,'Smith','John',12220
234,'Jones','Mary',12221

0,1001,3,12220
0,1002,2,12220
0,1001,8,12221
0,1003,5,12221

Now you can insert Orders, with the correct relationship to what got
inserted.

You'll have to do some basic math on the @@IDENTITY, but you figure it out.

The inserts, basic math/updates will have to be in a TRANS, or you'll screw
yourself.
I hope that kinda makes sense. I've given a loose guideline, not an exact
solution. The key is to ... have some kind of relationship between Customer
and Order (OrdinalID) before you push the data in.
THEN insert the customers, find the @@IDENTITY, do some
math/updates........and inserts on the secondary (Orders) table.

...

"Magnus" <no@spam.comwrote in message news:45********@news.broadpark.no...
I'm testing walkthrough saving data to a Database (Multiple Tables).

http://msdn2.microsoft.com/en-us/lib...b4(VS.80).aspx

In the famous Customer/Order example, I'm getting referential integrity
problems "The INSERT statement conflicted with the FOREIGN KEY
constraint..." if the identity/primary key column is "out of sync" with
the
identity value in the database. I can easily acheive this f.ex. by
pressing
add new record, cancel then add again and filling out customer and order
details. Obviously the customerId column in the order table has the wrong
values after the customer table has been inserted, as the customerid in
the
customer table is updated to whatever number the database has given to the
record. Should I now update the columns referencing the customerID field
manually by code? Or how is a good way to do this?

I can't find any articles on how this can be effectively handled, can
anyone
give me some pointers or give an example on how this should/could be
handled?

- Magnus


Nov 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
6
by: S. Graefner | last post by:
Hello Folks, I'm a Relitive newbie to the blood sport of MS ACCESS. I have designed and built a database with multiple, Related, tables. The db works well, more by good luck then good planing I...
13
by: Stuart McGraw | last post by:
I haven't been able to figure this out and would appreciate some help... I have two tables, both with autonumber primary keys, and linked in a conventional master-child relationship. I've...
11
by: Hi5 | last post by:
Hi, I am new to access I usedto work in Oracle and Mysql. I am after a way that enables me to populate a database I designed in access with lots of data which can be sorted in excel sheets, ...
5
by: mimo | last post by:
Hello, I have seen samples on how to pull data from one table and save back to it using the Form View control. How do I pull from multiple tables and save back to multiple tables on one...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
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...
9
by: karenjfrancis | last post by:
I have 4 Access databases, all with the same data model but different data. I want to build a front end that brings all of the data in the 4 databases together into one. Assuming my table of...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.