returning auto generated primary key RRS feed

  • Question

  • User1329325748 posted
    hey guys, i'm having a problem retrieving the auto generated primary key (generated on the database) back to the aspx page. first i created the record on aspx page using dataset then i added new row to the table and fill up the blank spaces, i left out the primary key cause it will get generated on the database. after i insert into the database, i need the primary key that was generated so i can process the sub records of the recored i just inserted. is there any way to get the number that was just generated? i know there is a function to set a auto number on a dataset but how can set starting value is same with the data on the server? is there any solution for this?? thank you!!
    Friday, October 24, 2003 2:41 AM

All replies

  • User1536088975 posted
    You can return the new autoincrement value by using a stored procedure output parameter. If your using SQL Server you can use the @@Identity or the SCOPE_IDENTITY. These return the last identity values generated by your connection to the database. Here is an example of a stored procedure which inserts a record and returns the identity (primary key) value: CREATE PROCEDURE spOrdersInsert (@OrderID int Output, @CustomerID int, @EmployeeID int, @OrderDate datetime) A AS INSERT INTO Orders (CustomerID, EmployeeID, OrderDate) VALUES (@CustomerID, @EmployeeID, @OrderDate) SELECT @OrderID = @@IDENTITY Hope this helps Regards Basia
    Friday, October 24, 2003 5:40 AM
  • User1329325748 posted
    thank you for your reply basia, however since the when i insert the data into the table, the table that i want to insert gets generated dynamically, the process can be loop inside the loop since there are parent record and sub records. Is there anyway of updating the auto number in the dataset that is identically same with the database table?? i was trying to use dataset.fillschema(), will that can be help in anyway??
    Sunday, October 26, 2003 4:53 PM