473,386 Members | 1,726 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.

Getting primary key from new record instered into one table using ADO.net

Hello,

I have a situation where I have a normalized table (Table1=InvoiceHeader,
Table2=invoicedetails). When I create an InvoiceHeader and Details, I'd
like to save the invoice header first, then associated the detail records
with that invoice by storing the InvoiceHeaders primary key (an incrimental
identifier) in the invoiceDetails table. How do I go about this?

THanks!
Feb 27 '06 #1
3 2489
This is a snippet of how I do it with VB6, connecting using ADO:
rs is a recordset, cn is a connection, cmd a command
With cmd
set .ActiveConnection = cn
.CommandType = adCmdText
.CommandText = mySQL ' the insert string

' Run the command (perform the Insert)
.Execute , , adExecuteNoRecords
If Err.Number <> 0 Then GoTo ErrorHandler

' Get the new AutoNumber value
Set rs = cn.Execute("SELECT @@Identity", , adCmdText)
AutoNumberValue = rs(0).Value
End With

I haven't done this in ADO.NET/VB.NET yet so I don't know if it works...

If this doesn't work for you I would suggest you:
(a) post back with details of your database type and how you connect to it.
(b) alternatively you could consider using a GUID for the PK approach.
That way you define the PK before you perform your insert and therefore
you already have it for the associated inserts -- you don't need to get
it back from the database, just know whether or not your insert was
successful.

Rico wrote:
Hello,

I have a situation where I have a normalized table (Table1=InvoiceHeader,
Table2=invoicedetails). When I create an InvoiceHeader and Details, I'd
like to save the invoice header first, then associated the detail records
with that invoice by storing the InvoiceHeaders primary key (an incrimental
identifier) in the invoiceDetails table. How do I go about this?

THanks!

Feb 27 '06 #2
Yes, the @@IDENITY works in ADO.Net at least for Access DataBases. I think
SQL databases have a different approach.
--
Dennis in Houston
"Gman" wrote:
This is a snippet of how I do it with VB6, connecting using ADO:
rs is a recordset, cn is a connection, cmd a command
With cmd
set .ActiveConnection = cn
.CommandType = adCmdText
.CommandText = mySQL ' the insert string

' Run the command (perform the Insert)
.Execute , , adExecuteNoRecords
If Err.Number <> 0 Then GoTo ErrorHandler

' Get the new AutoNumber value
Set rs = cn.Execute("SELECT @@Identity", , adCmdText)
AutoNumberValue = rs(0).Value
End With

I haven't done this in ADO.NET/VB.NET yet so I don't know if it works...

If this doesn't work for you I would suggest you:
(a) post back with details of your database type and how you connect to it.
(b) alternatively you could consider using a GUID for the PK approach.
That way you define the PK before you perform your insert and therefore
you already have it for the associated inserts -- you don't need to get
it back from the database, just know whether or not your insert was
successful.

Rico wrote:
Hello,

I have a situation where I have a normalized table (Table1=InvoiceHeader,
Table2=invoicedetails). When I create an InvoiceHeader and Details, I'd
like to save the invoice header first, then associated the detail records
with that invoice by storing the InvoiceHeaders primary key (an incrimental
identifier) in the invoiceDetails table. How do I go about this?

THanks!

Feb 28 '06 #3
Hi GMan,

Thanks. It will take me some fighting through the variables you have here
(sorry I'm a newbie) to see if I can get it to work, but will let you know.
I'm also unfamiliar with the "PK approach" (did I mention I was a newbie?)

I'm using an Access database as a back end. Right now it's using an ADO.net
connection built "from scratch". To this point, all that's been required is
just updating stand alone tables.

Any help would be great.

Thanks!
"Gman" <nah> wrote in message news:Og*************@TK2MSFTNGP09.phx.gbl...
This is a snippet of how I do it with VB6, connecting using ADO:
rs is a recordset, cn is a connection, cmd a command
With cmd
set .ActiveConnection = cn
.CommandType = adCmdText
.CommandText = mySQL ' the insert string

' Run the command (perform the Insert)
.Execute , , adExecuteNoRecords
If Err.Number <> 0 Then GoTo ErrorHandler

' Get the new AutoNumber value
Set rs = cn.Execute("SELECT @@Identity", , adCmdText)
AutoNumberValue = rs(0).Value
End With

I haven't done this in ADO.NET/VB.NET yet so I don't know if it works...

If this doesn't work for you I would suggest you:
(a) post back with details of your database type and how you connect to
it.
(b) alternatively you could consider using a GUID for the PK approach.
That way you define the PK before you perform your insert and therefore
you already have it for the associated inserts -- you don't need to get it
back from the database, just know whether or not your insert was
successful.

Rico wrote:
Hello,

I have a situation where I have a normalized table (Table1=InvoiceHeader,
Table2=invoicedetails). When I create an InvoiceHeader and Details, I'd
like to save the invoice header first, then associated the detail records
with that invoice by storing the InvoiceHeaders primary key (an
incrimental identifier) in the invoiceDetails table. How do I go about
this?

THanks!

Mar 1 '06 #4

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

Similar topics

5
by: Dave Sisk | last post by:
Hey folks: I'm trying to do this: CREATE TRIGGER datawhse.emp_ti AFTER INSERT ON emp REFERENCING NEW AS n FOR EACH ROW MODE DB2ROW BEGIN DECLARE v_rrn DECIMAL(15,0);
7
by: Ilan Sebba | last post by:
I am trying to add a record using SQL. My problem is that the primary keys are foreign keys, and these foreign keys are autonumbers. I therefore do not know the primary keys of the record I am...
7
by: Philip | last post by:
Hey all, (Access 2000) I've been having a horror story with this design problem. My Database is Structured like This: AUTHORS, BOOKS, PAGES. Those are the Tables and each Item in each table...
2
by: stranger | last post by:
My database is set up so people can input parts orders. Sometimes they order the same parts on a monthly basis. I want to be able to duplicate past parts orders and have it pasted in with a new...
5
by: Jason | last post by:
I've been trying to figure out a good way to do this but haven't had much luck, any input would be greatly appreciated. Basically, after a datagrid is sorted, how can I get the primary key value...
8
by: Paul Hunter | last post by:
I am new to databases and thus to Access. I have a situation where I am trying to figure out how to key some tables I am working with. Consider that I have a database of my own records which are...
5
by: Dwight | last post by:
Hi all, Iam a first time user of any database. I am using A2003. I want to design a database for county marriage records. So far, I have the following tables: COUNTY: County GROOM:...
1
by: sparks | last post by:
I have a form/table with an autoid it is linked to a table/form with and ID as a 1 to many. Under this form/table I need another table with many records for each on of the many in the previous...
3
by: Markw | last post by:
I think this was recently asked but I was a little lost on the example that was used so I'm reasking it with an example I can understand. Also forgive me for such a basic question but I really am...
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
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
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:
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.