0

I am looking to Insert a row into a table and return the Identity column in c# code. I cannot seem to get the syntax quite right.

Here is the storted procedure

ALTER PROCEDURE [dbo].[sp_InsertIssue]

       @Application                       nchar(20)      = NULL  , 
       @Version                     nchar(10)              = NULL    , 
       @CreatedBy                       NVARCHAR(30)       = NULL  , 
       @AssignedTo                    nVARCHAR(max)         = NULL  , 
       @Description                         nVARCHAR(max)   = NULL  , 
       @UserId                    INT                   
AS 
BEGIN 
     SET NOCOUNT ON 

     INSERT INTO dbo.Issue
          ( 

            Application                     ,
            Version                   ,
            CreatedBy                     ,
            AssignedTo                   ,
            Description                          ,
            UserId                  
          ) 

     VALUES 
          ( 

       @Application                         , 
       @Version                          , 
       @CreatedBy                       , 
       @AssignedTo                      , 
       @Description                           , 
       @UserId                  
          ) 

         RETURN SCOPE_IDENTITY() 
END

Here is the C# Code

SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "sp_InsertIssue ";
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Connection = m_Connection;

        SqlParameter parm = new SqlParameter("@IssueId", SqlDbType.Int);
        parm.Direction = ParameterDirection.ReturnValue;


        cmd.Parameters.Add("@Application", SqlDbType.VarChar).Value = p_Application;
        cmd.Parameters.Add("@Version", SqlDbType.VarChar).Value = p_Version;
        cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = p_CreatedBy;
        cmd.Parameters.Add("@AssignedTo", SqlDbType.VarChar).Value = p_AssignedTo;
        cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = p_Description;
        cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = p_UserId;

        var returnParameter = cmd.Parameters.Add("IssueId", SqlDbType.Int);
        returnParameter.Direction = ParameterDirection.ReturnValue;

        /// send data to db
        Int32 id = (int)cmd.ExecuteNonQuery();

Return -1 not the identity column

2
  • 1
    stackoverflow.com/questions/13416811/… Commented Aug 1, 2016 at 16:06
  • Two issues here. First you should NOT use the return value to return data. It is intended to return a status of the execution. You should use an OUTPUT parameter instead. Second issue is the sp_ prefix. You should change the prefix to something else, or even better remove the prefix entirely. sqlperformance.com/2012/10/t-sql-queries/sp_prefix Commented Aug 1, 2016 at 16:43

2 Answers 2

1

You have to make some changes :

  1. SELECT SCOPE_IDENTITY() is beter than Return SCOPE_IDENTITY()
  2. in your code you have to change also cmd.ExecuteNonQuery() with int id = Convert.ToInt32(cmd.ExecuteScalar());
Sign up to request clarification or add additional context in comments.

Comments

1

Changing my answer completely after reading your code more closely :) You were so close

Change:

Int32 id = (int)cmd.ExecuteNonQuery();

To:

cmd.ExecuteNonQuery(); // this returns the # of "rows affected"
Int32 id = (int)returnParameter.Value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.