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

  • Home
  • SEARCH
  • 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 82227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:35:02+00:00 2026-05-10T21:35:02+00:00

I am currently working in C#, and I need to insert a new record

  • 0

I am currently working in C#, and I need to insert a new record into one table, get the new primary key value, and then use that as a foreign key reference in inserting several more records. The Database is MS SQL Server 2003. All help is appreciated!

  • 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. 2026-05-10T21:35:03+00:00Added an answer on May 10, 2026 at 9:35 pm

    The way to get the identity of the inserted row is with the SCOPE_IDENTITY() function. If you’re using stored procedures then this would look something like the following to return the row identity as an output parameter.

    CREATE PROCEDURE dbo.MyProcedure (     @RowId INT = NULL OUTPUT ) AS  INSERT INTO MyTable (     Column1     ,Column2     ,... ) VALUES (     @Param1     ,@Param2     ,... );  SET @RowId = SCOPE_IDENTITY(); 

    You can then use this value for any subsequent inserts (alternatively, if you can pass the data all into the stored procedure, then you can use it in the remainder of the procedure body).

    If you’re passing the SQL in dynamically then you use much the same technique, but with a single string with statement delimiters (also ; in SQL), e.g.:

    var sql = 'INSERT INTO MyTable (Column1, Column2, ...) VALUES (@P1, @P2, ...);' +           'SELECT SCOPE_IDENTITY();'; 

    Then if you execute this using ExecuteScalar you’ll be able to get the identity back as the scalar result and cast it to the right type. Alternatively you could build up the whole batch in one go, e.g.

    var sql = 'DECLARE @RowId INT;' +            'INSERT INTO MyTable (Column1, Column2, ...) VALUES (@P1, @P2, ...);' +           'SET @RowId = SCOPE_IDENTITY();' +           'INSERT INTO MyOtherTable (Column1, ...) VALUES (@P3, @P4, ...);'; 

    This may not be exactly the right syntax, and you may need to use SET NOCOUNT ON; at the start (my mind is rusty as I rarely use dynamic SQL) but it should get you on the right track.

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

Sidebar

Ask A Question

Stats

  • Questions 142k
  • Answers 142k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Bouncy Castle is a well respected and well developed .NET… May 12, 2026 at 8:14 am
  • Editorial Team
    Editorial Team added an answer In general removeChild has to be called from the parent… May 12, 2026 at 8:14 am
  • Editorial Team
    Editorial Team added an answer You check referrer and parse out the words. Most likely… May 12, 2026 at 8:14 am

Related Questions

I am currently working on a VB.NET desktop application that uses .mdb (Access) database
I am working with an open-source UNIX tool that is implemented in C++, and
I am currently working for a client who are petrified of changing lousy un-testable
I am working on a project for an IT class where I need to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.