473,783 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

master/detail foreign keys during insert

when I insert a new record into a master record - I need to update the new child record with the autonumbered ID key from the master recor

Is there a simple way to do this? I insert the record - then try to find it - then read the key and that seems silly

thx for your assistance
Jul 21 '05 #1
4 2370
Set up a DataRelation on this field...
http://www.knowdotnet.com/articles/datarelation.html Then when you set the
parent value the child values will be updated accordingly. Typically, you'd
set the autoincrement value to -1 and define a datarelation between the
fields. When the update is submitted, the DB will assign a value to the
negative number since autoincrement fields don't allow negatives. You can
then refresh the dataset with another select statement or specify this
option in the DataAdapter Configuration wizard...it's on by default and all
it does is append an additional Select statement on the end of your inserts.

When the value is updated from the DB, the parent record will be corrected.
then if a relation is set properly, the child values will be set too...now
you can submit the updaate on the child table.

HTH,

Bill
"mekim" <an*******@disc ussions.microso ft.com> wrote in message
news:3F******** *************** ***********@mic rosoft.com...
when I insert a new record into a master record - I need to update the new child record with the autonumbered ID key from the master record
Is there a simple way to do this? I insert the record - then try to find it - then read the key and that seems silly -
thx for your assistance

Jul 21 '05 #2
Set up a DataRelation on this field...
http://www.knowdotnet.com/articles/datarelation.html Then when you set the
parent value the child values will be updated accordingly. Typically, you'd
set the autoincrement value to -1 and define a datarelation between the
fields. When the update is submitted, the DB will assign a value to the
negative number since autoincrement fields don't allow negatives. You can
then refresh the dataset with another select statement or specify this
option in the DataAdapter Configuration wizard...it's on by default and all
it does is append an additional Select statement on the end of your inserts.

When the value is updated from the DB, the parent record will be corrected.
then if a relation is set properly, the child values will be set too...now
you can submit the updaate on the child table.

HTH,

Bill
"mekim" <an*******@disc ussions.microso ft.com> wrote in message
news:3F******** *************** ***********@mic rosoft.com...
when I insert a new record into a master record - I need to update the new child record with the autonumbered ID key from the master record
Is there a simple way to do this? I insert the record - then try to find it - then read the key and that seems silly -
thx for your assistance

Jul 21 '05 #3
I just posted this...this seems to wor

Any suggestions

Public Sub AddingParentChi ldRecords(
Dim dbConn As New OleDbConnection (constConnectio nString
dbConn.Open(

Dim dbSet As New DataSe
Dim dbParentAdapt As OleDbDataAdapte
dbParentAdapt = New OleDbDataAdapte r("SELECT * FROM TableParent", dbConn

Dim dbParentCB As New OleDbCommandBui lder(dbParentAd apt
dbParentAdapt.F illSchema(dbSet , SchemaType.Sour ce

AddHandler dbParentAdapt.R owUpdated, AddressOf dbParentAdapt_O nRowUpdat

Dim dbTable As DataTable = dbSet.Tables("T able"
dbTable.TableNa me = "TableParen t

Dim dbChildAdapt As New OleDbDataAdapte r("SELECT * FROM TableChild", dbConn
Dim dbChildCB As New OleDbCommandBui lder(dbChildAda pt
dbChildAdapt.Fi llSchema(dbSet, SchemaType.Sour ce

dbTable = dbSet.Tables("T able"
dbTable.TableNa me = "TableChild

Dim dr As New DataRelation("P arentChild",
dbSet.Tables("T ableParent").Co lumns("ParentId "),
dbSet.Tables("T ableChild").Col umns("ParentId" )

dbSet.Relations .Add(dr

Dim oParentRow As DataRow = dbSet.Tables("T ableParent").Ne wRow(
oParentRow("Par entText") = Now().ToStrin
dbSet.Tables("T ableParent").Ro ws.Add(oParentR ow

Dim oChildRow As DataRow = dbSet.Tables("T ableChild").New Row(
oChildRow("Chil dText") = Now().Ticks.ToS trin

oChildRow.SetPa rentRow(oParent Row
dbSet.Tables("T ableChild").Row s.Add(oChildRow

dbParentAdapt.U pdate(dbSet, "TableParen t"
dbChildAdapt.Up date(dbSet, "TableChild "

dbConn.Close(
End Su
Jul 21 '05 #4
I just posted this...this seems to wor

Any suggestions

Public Sub AddingParentChi ldRecords(
Dim dbConn As New OleDbConnection (constConnectio nString
dbConn.Open(

Dim dbSet As New DataSe
Dim dbParentAdapt As OleDbDataAdapte
dbParentAdapt = New OleDbDataAdapte r("SELECT * FROM TableParent", dbConn

Dim dbParentCB As New OleDbCommandBui lder(dbParentAd apt
dbParentAdapt.F illSchema(dbSet , SchemaType.Sour ce

AddHandler dbParentAdapt.R owUpdated, AddressOf dbParentAdapt_O nRowUpdat

Dim dbTable As DataTable = dbSet.Tables("T able"
dbTable.TableNa me = "TableParen t

Dim dbChildAdapt As New OleDbDataAdapte r("SELECT * FROM TableChild", dbConn
Dim dbChildCB As New OleDbCommandBui lder(dbChildAda pt
dbChildAdapt.Fi llSchema(dbSet, SchemaType.Sour ce

dbTable = dbSet.Tables("T able"
dbTable.TableNa me = "TableChild

Dim dr As New DataRelation("P arentChild",
dbSet.Tables("T ableParent").Co lumns("ParentId "),
dbSet.Tables("T ableChild").Col umns("ParentId" )

dbSet.Relations .Add(dr

Dim oParentRow As DataRow = dbSet.Tables("T ableParent").Ne wRow(
oParentRow("Par entText") = Now().ToStrin
dbSet.Tables("T ableParent").Ro ws.Add(oParentR ow

Dim oChildRow As DataRow = dbSet.Tables("T ableChild").New Row(
oChildRow("Chil dText") = Now().Ticks.ToS trin

oChildRow.SetPa rentRow(oParent Row
dbSet.Tables("T ableChild").Row s.Add(oChildRow

dbParentAdapt.U pdate(dbSet, "TableParen t"
dbChildAdapt.Up date(dbSet, "TableChild "

dbConn.Close(
End Su
Jul 21 '05 #5

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

Similar topics

5
49735
by: Olivier Crèvecoeur | last post by:
Hello, Excuse me for my poor english. I would kike know if create index on the foreign key it's necessary or if Oracle, are optimized for using foreign key whithout index. Best regards Olivier
1
3006
by: Vinodh Kumar P | last post by:
I understand the number of foreign keys allowed is restricted by the DBMS I use. In a general relational schema design perspective how many foreign keys a table shall have? If I have large number of foreign keys what anamolies it will lead to? Is this crucial to identify all the foriegn key relationships for a table? Vinodh
1
459
by: Juan | last post by:
I built a form that displays a master-detail relation ship, now i need to add a column to the datagrid displaying the master data, this column corresponds to the text name of a column stored in the master table as an integer (the foreign key, i need to display the text name of the foreign key stored in the third table). Do i have to create another datatable in the dataset im using that includes all the information I need? Change the Sql...
0
1418
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A stored procedure updates a row in C, adds a row each in B & C. I get an integrity violation. All the foreign keys are deferrable, and the stored procedure is called from within a transaction with constraints deferred. (And the foreign keys do refer to...
3
289
by: mekim | last post by:
when I insert a new record into a master record - I need to update the new child record with the autonumbered ID key from the master recor Is there a simple way to do this? I insert the record - then try to find it - then read the key and that seems silly thx for your assistance
1
2097
by: Thomas T. Thai | last post by:
I'm looking for a better way to make use of foreign keys. Here is a sample setup: -- TESTING Foreign Keys create table mod ( mod_id int not null primary key, name varchar(32) not null default '' );
1
12063
by: Wes Groleau | last post by:
INSERT INTO X ...... ( A, B, C ) INSERT INTO Y ...... ( J, K, L ) If Y has a foreign key M which is the primary key D of X, is there an easy and/or efficient way to have SQL Server assign D, and tell us what it is so we can add M to the list for Y ? I know I can select D where A, B, C but I wondered about other tricks.
7
4539
by: john | last post by:
In my form I have a master table and a details table linked 1xM. I can search through the whole parent table but I also like to be able to search through the child table fields to find parent records. Should I design a new form for this or can I somehow make this work in the same form. Thanks in advance, john
1
2346
Echooff3
by: Echooff3 | last post by:
My application utilizes a dataset with a couple of One to Many relations. If the user adds a new record and adds information to the master part of the form and adds information to the detail part of the form then clicks save. The master record gets saved and the detail throws a “INSERT statement conflicted with the FOREIGN KEY constraint … The conflict occurred in “MasterTable”, “MasterTableID”. My save routine goes like this: Me.Validate...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.