Groups | Blog | Home
all groups > asp.net > march 2006 >

asp.net : getting the value of the primary key on an insert


evandela NO[at]SPAM bigpond.net.au
3/22/2006 11:40:09 PM
I am having pretty much of the same problem but with an access
Database. When I insert a new product, I need to update a second
reference table with the primary key of the new insert. I have included
my code below to make it a little clearer... if anyone can offer me any
help here - it would be REALLY appreciated.


Protected Sub addNew_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

AccessDataSource1.InsertCommand = "INSERT INTO product
(productName,productPartNumber,productNSNNumber,productDescription)
VALUES ('New Product','1234567890','1234567890','New Product
Description')"

AccessDataSource1.Insert()

AccessDataSource1.InsertCommand = "INSERT INTO
countryProductCost(countryID,productID,cost) VALUES ('1',' <NEED TO
INSERT THE primary key of the insert performed above here> ','0')"


End Sub
</script>

<asp:Button ID="btnAddProduct" runat="server" Text="Add Product"
OnClick="btnAddProduct_Click" /><br />

<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="App_Data\products.mdb" />

Anyone have any ideas, they would be really appreciated.
Patrick.O.Ige
3/23/2006 12:00:00 AM
If i understood your question have you looked at
Scope identity()
Patrick

[quoted text, click to view]

clickon
3/23/2006 1:02:32 AM
I too would like an answer to this as it is very easy to do in PHP. If you
used SQL Server instead you could set up a trigger to update the secobnd
table when a record was inserted in the first table, but i don't think access
supports triggers.

[quoted text, click to view]
Mark Rae
3/23/2006 10:10:48 AM
[quoted text, click to view]

1) Launch your Internet browser (IE, FireFox, Netscape etc)

2) Navigate to http://www.google.com

3) Enter the text below in the box:

Access Identity Insert

4) Hit the button

AddThis Social Bookmark Button