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 6851793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:17:28+00:00 2026-05-27T01:17:28+00:00

How do I perform an insert to database and return inserted identity with Dapper?

  • 0

How do I perform an insert to database and return inserted identity with Dapper?

I’ve tried something like this:

string sql = "DECLARE @ID int; " +
             "INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff); " +
             "SELECT @ID = SCOPE_IDENTITY()";

var id = connection.Query<int>(sql, new { Stuff = mystuff}).First();

But it did’t work.

@Marc Gravell thanks, for reply.
I’ve tried your solution but, still same exception trace is below

System.InvalidCastException: Specified cast is not valid

at Dapper.SqlMapper.<QueryInternal>d__a`1.MoveNext() in (snip)\Dapper\SqlMapper.cs:line 610
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in (snip)\Dapper\SqlMapper.cs:line 538
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param) in (snip)\Dapper\SqlMapper.cs:line 456
  • 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-27T01:17:29+00:00Added an answer on May 27, 2026 at 1:17 am

    It does support input/output parameters (including RETURN value) if you use DynamicParameters, but in this case the simpler option is simply:

    var id = connection.QuerySingle<int>( @"
    INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff);
    SELECT CAST(SCOPE_IDENTITY() as int)", new { Stuff = mystuff});
    

    Note that on more recent versions of SQL Server (2005+) you can use the OUTPUT clause:

    var id = connection.QuerySingle<int>( @"
    INSERT INTO [MyTable] ([Stuff])
    OUTPUT INSERTED.Id
    VALUES (@Stuff);", new { Stuff = mystuff});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to perform a simple INSERT and return the identity (auto-incrementing primary key).
I would like to perform a scalar database query and return the result into
When I perform a select/Insert query, does SQL Server automatically create an implicit transaction
I am trying to perform a bulk insert onto SQL Server: BULK INSERT SampleData
How can I perform an idempotent insert row using subsonic with a SQL 2008
I need to perform a very large sql server insert from a c# application.
I'm using Linq to SQL for the Database operations, and am trying to perform
I would like to know the command to perform a mysqldump of a database
I need to perform update/insert simultaneously changing structure of incoming data. Think about Shops
I'm basically trying to figure out the simplest way to perform your basic insert

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.