Solved

Use New Inserted Autonumber ID as session variable

Posted on 2012-03-27
1
389 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now