Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use New Inserted Autonumber ID as session variable

Posted on 2012-03-27
1
Medium Priority
?
401 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 29

Accepted Solution

by:
sammySeltzer earned 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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:…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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