Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7497733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:12:42+00:00 2026-05-29T19:12:42+00:00

Given the following code (which is mostly irrelevant except for the last two lines),

  • 0

Given the following code (which is mostly irrelevant except for the last two lines), what would your method be to get the value of the identity field for the new record that was just created? Would you make a second call to the database to retrieve it based on the primary key of the object (which could be problematic if there’s not one), or based on the last inserted record (which could be problematic with multithreaded apps) or is there maybe a more clever way to get the new value back at the same time you are making the insert?

Seems like there should be a way to get an Identity back based on the insert operation that was just made rather than having to query for it based on other means.

public void Insert(O obj)
{
    var sqlCmd = new SqlCommand() { Connection = con.Conn };
    var sqlParams = new SqlParameters(sqlCmd.Parameters, obj);
    var props = obj.Properties.Where(o => !o.IsIdentity);

    InsertQuery qry = new InsertQuery(this.TableAlias);
    qry.FieldValuePairs = props.Select(o => new SqlValuePair(o.Alias, sqlParams.Add(o))).ToList();

    sqlCmd.CommandText = qry.ToString();
    sqlCmd.ExecuteNonQuery();
}

EDIT: While this question isn’t a duplicate in the strictest manner, it’s almost identical to this one which has some really good answers: Best way to get identity of inserted row?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:12:43+00:00Added an answer on May 29, 2026 at 7:12 pm

    It strongly depends on your database server. For example for Microsoft SQL Server you can get the value of the @@IDENTITY variable, that contains the last identity value assigned.

    To prevent race conditions you must keep the insert query and the variable read inside a transaction.

    Another solution could be to create a stored procedure for every type of insert you have to do and make it return the identity value and accept the insert arguments.

    Otherwise, inside a transaction you can implement whatever ID assignment logic you want and be preserved from concurrency problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following lines of code which is using JQTOUCH: $('#customers').bind('pageAnimationEnd', function(e, info){ if
Given the following code, I would expect an alert of searchRes1 which is a
Given the following code which I can't get to compile. template < typename OT,
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
Given the following code which creates an and condition, how do I make it
Given the following C++ code which doesn't mention a calling convention in the function
Given this following sample code which clones a table row, sets some properties and
I have the following code which I'm using to get the user to input
I have the following piece of code which attempts to determine whether a given
Given the following code which works: for (i=0; i<nLinears; i++) { for (j=0; j<nLinearPts[i]-1;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.