Solved

Use New Inserted Autonumber ID as session variable

Posted on 2012-03-27
1
392 Views
Last Modified: 2012-04-03
I am usually a PHP guy so I know basically nothing about .NET.  I am using an INSERT Record Behavior in Dreamweaver (I know I should be using VS but DW is easier for me to navigate).  I am using the basic insert record behavior and need to take that new IDENTITY line, store it in a session variable, and use it on the next page to insert it in another table with a FK so the tables relate.

I need code as again, I am new with .NET.  I have found some articles on the web in forums to create a stored procedure, etc. but I think that would be too complicated for what I need.  I have also been looking at Identity_Scope() but not sure I want to use that either.

So I need to create a new session variable with the new ID then use that ID on the next INSERT page and insert that ID into the database FK.  Thank you all!
0
Comment
Question by:palmtreeinfotech
1 Comment
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 37796802
What's wrong with using SCOPE_IDENTITY()?

Assume this is your insert:

INSERT INTO [Products] ([ProductName], [CategoryID], [UnitPrice], [Discontinued]) VALUES (@ProductName, @CategoryID, @UnitPrice, @Discontinued)

'Grab the last inserted ID and assign it into a variable called @NewProductID

SELECT @NewProductID = SCOPE_IDENTITY()

Now, make it a session variable to be used in another page.

Session.Item("NewID") = @NewProductID

Then you should be able to use it on any page.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question