Monday, March 12, 2012

Requesting it(PK) after saving data with auto Prim.Key

Hi,

I have a page for saving a "home" data. As the page unloads I willredirect the user to the page on which he can add the photos to this"home".

Well I structured my photo adding page with first requesting the "homeID" which is an auto Integer (PK).
Because it's renaming the uploaded photos with the homeID and _01, _02...

Because of that i need to send the user to the photo adding page with "response.redirect.....aspx?homeID=....

homeID= ?

But I don't know how to get the homeID value, that the SQL Server automaticly selects for the data. As it is just saving it...

Thanks a lot.If you are using Stored Procedures, make an output parameter for your new ID, like

@.NewHomeId INT OUTPUT

then right after you do your insert

do

SET @.NewHomeId = SCOPE_IDENTITY()

and the value will be returned with the parameters. on the data layer, you can do

(assuming you output parmetere is the last in the params)
int val = (int)parms[parms.length - 1].Value;

No comments:

Post a Comment