Scope Identity:  Retrieve Record ID after Insert

Posted on 2009-04-27
Last Modified: 2013-12-17

I have a form that inserts into a database table succesfully.  After the insert, I want to display some information about the newly inserted record on a results page.  I thought about query string, session etc.  

I already retrieve the cope identity in the procedure.

I am working in c#.
Question by:devnewbee
    1 Comment
    LVL 3

    Accepted Solution

    Either a querystring or a session variable would work fine.  If using a querystring, the user can see the content which is okay as long as you feel this is not a security issue.  Once the data is saved, then simply response.redirect("yourresultpage.aspx?id=" + _scope_identity_id_from_db).  The only problem with a session variable is that if the page is refreshed after the server timeout period, then the id held in the session disappears and your code would fail.  As the querystring retains all the data it needs, timeout might not be such an issue (although if your code uses other time-dependant variables then yoou have the same issue).  The benefit of the session over the response.redirect method is that you can use server.transfer after you store the id of the record in the session, saving a round-trip to the server.

    Moving on... in the "yourresultpage.aspx" - the attached code snippet opens the database, the command object and the datareader to display the record.

    I hope I have understood your question correctly.
    protected void Page_Load(object sender, System.EventArgs e)
    	if (!page.ispostback()) {
    		string _id = Request.QueryString("id");
    		using (SqlClient.SqlConnection _o = new SqlClient.SqlConnection("myconnectionstring...")) {
    			using (SqlClient.SqlCommand _c = new SqlClient.SqlCommand()) {
    				_c.Connection = _o;
    				_c.CommandText = "SELECT * FROM MyTable WHERE ID=" + _id;
    				_c.CommandType = CommandType.Text;
    				using (SqlClient.SqlDataReader _r = .ExecuteReader) {
    					if (_r.Read) {
    						// Fill your form from the reader:
    						txtMyField.text = _r("MyField").ToString;

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    754 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

    24 Experts available now in Live!

    Get 1:1 Help Now