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

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 2343
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*******@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.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*******@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.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 AddingParentChildRecords(
Dim dbConn As New OleDbConnection(constConnectionString
dbConn.Open(

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

Dim dbParentCB As New OleDbCommandBuilder(dbParentAdapt
dbParentAdapt.FillSchema(dbSet, SchemaType.Source

AddHandler dbParentAdapt.RowUpdated, AddressOf dbParentAdapt_OnRowUpdat

Dim dbTable As DataTable = dbSet.Tables("Table"
dbTable.TableName = "TableParent

Dim dbChildAdapt As New OleDbDataAdapter("SELECT * FROM TableChild", dbConn
Dim dbChildCB As New OleDbCommandBuilder(dbChildAdapt
dbChildAdapt.FillSchema(dbSet, SchemaType.Source

dbTable = dbSet.Tables("Table"
dbTable.TableName = "TableChild

Dim dr As New DataRelation("ParentChild",
dbSet.Tables("TableParent").Columns("ParentId"),
dbSet.Tables("TableChild").Columns("ParentId")

dbSet.Relations.Add(dr

Dim oParentRow As DataRow = dbSet.Tables("TableParent").NewRow(
oParentRow("ParentText") = Now().ToStrin
dbSet.Tables("TableParent").Rows.Add(oParentRow

Dim oChildRow As DataRow = dbSet.Tables("TableChild").NewRow(
oChildRow("ChildText") = Now().Ticks.ToStrin

oChildRow.SetParentRow(oParentRow
dbSet.Tables("TableChild").Rows.Add(oChildRow

dbParentAdapt.Update(dbSet, "TableParent"
dbChildAdapt.Update(dbSet, "TableChild"

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

Any suggestions

Public Sub AddingParentChildRecords(
Dim dbConn As New OleDbConnection(constConnectionString
dbConn.Open(

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

Dim dbParentCB As New OleDbCommandBuilder(dbParentAdapt
dbParentAdapt.FillSchema(dbSet, SchemaType.Source

AddHandler dbParentAdapt.RowUpdated, AddressOf dbParentAdapt_OnRowUpdat

Dim dbTable As DataTable = dbSet.Tables("Table"
dbTable.TableName = "TableParent

Dim dbChildAdapt As New OleDbDataAdapter("SELECT * FROM TableChild", dbConn
Dim dbChildCB As New OleDbCommandBuilder(dbChildAdapt
dbChildAdapt.FillSchema(dbSet, SchemaType.Source

dbTable = dbSet.Tables("Table"
dbTable.TableName = "TableChild

Dim dr As New DataRelation("ParentChild",
dbSet.Tables("TableParent").Columns("ParentId"),
dbSet.Tables("TableChild").Columns("ParentId")

dbSet.Relations.Add(dr

Dim oParentRow As DataRow = dbSet.Tables("TableParent").NewRow(
oParentRow("ParentText") = Now().ToStrin
dbSet.Tables("TableParent").Rows.Add(oParentRow

Dim oChildRow As DataRow = dbSet.Tables("TableChild").NewRow(
oChildRow("ChildText") = Now().Ticks.ToStrin

oChildRow.SetParentRow(oParentRow
dbSet.Tables("TableChild").Rows.Add(oChildRow

dbParentAdapt.Update(dbSet, "TableParent"
dbChildAdapt.Update(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
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 ...
1
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...
1
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...
0
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...
3
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 -...
1
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...
1
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,...
7
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...
1
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
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,...

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.