Weedoze
Weedoze

Reputation: 13943

c# stored procedure scope_identity

I have an SQL Server stored procedure which do an INSERT INTO.

I'm trying to get in C# the ID of the inserted row.

I can't debug so I don't know where is the error...

The row is perfectly add in the database but it always catch an exception and the ID is not returned

STORED PROCEDURE

USE [DatabaseAzerty]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[addLog] 
    -- Add the parameters for the stored procedure here
    @table varchar(max), 
    @date date,
    @version varchar(max),
    @process varchar(max),
    @level varchar(max),
    @message varchar(max),
    @stacktrace varchar(max),
    @user varchar(max),
    @environmentID varchar(max),
    @UUID varchar(max),
    @UDID varchar(max),
    @transactionID int,
    @new_identity int OUTPUT

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
 --   DECLARE @sql NVARCHAR(MAX);

 --   SET @sql = 'INSERT INTO dbo.' + @table + '([Timestamp], [Version],Process,[Level],[Message],StackTrace,[User],EnvironmentID,UUID,UDID,TransactionID) VALUES (' + CAST(@date AS VARCHAR(MAX)) + ',' + @version + ',' + @process + ','+ @level + ','+ @message + ','+ @stacktrace + ','+ @user + ','+ @environmentID + ','+ @UUID + ','+ @UDID + ',' + @transactionID + ')'
    --EXEC @sql
    DECLARE @SQL NVARCHAR(MAX);

SET @SQL =  ' INSERT INTO dbo.' + @Table;
SET @SQL += '   ([Timestamp], [Version],Process,[Level],[Message],StackTrace,[User],EnvironmentID,UUID,UDID,TransactionID)';
SET @SQL += ' VALUES';
SET @SQL += '   (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);';

EXECUTE sp_executesql
      @SQL
    , N'@p0 DATE, @p1 VARCHAR(MAX), @p2 VARCHAR(MAX), @p3 VARCHAR(MAX), @p4 VARCHAR(MAX), @p5 VARCHAR(MAX), @p6 VARCHAR(MAX), @p7 VARCHAR(MAX), @p8 VARCHAR(MAX), @p9 VARCHAR(MAX), @p10 int'
    , @p0 = @date
    , @p1 = @version
    , @p2 = @process
    , @p3 = @level
    , @p4 = @message
    , @p5 = @stacktrace
    , @p6 = @user
    , @p7 = @environmentID
    , @p8 = @UUID
    , @p9 = @UDID
    , @p10 = @transactionID;

    SELECT @new_identity =  CAST(SCOPE_IDENTITY() AS INT);
END

C#

 SqlConnection conn = new SqlConnection(connectionString);


            var cmd = new SqlCommand("addLog", conn)
            {
                CommandType = System.Data.CommandType.StoredProcedure
            };

            cmd.Parameters.Add(new SqlParameter("@message", message));

            SqlParameter id = new SqlParameter("@new_identity", SqlDbType.Int);
            id.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(id);

            conn.Open();
            cmd.ExecuteNonQuery();
            int returnedID = Convert.ToInt32(cmd.Parameters["@new_identity"].Value); 
            conn.Close();

            return returnedID;

Upvotes: 1

Views: 901

Answers (3)

M.Ali
M.Ali

Reputation: 69524

You need to pass the output parameter to your dynamic sql.

ALTER PROCEDURE [dbo].[addLog] 
    -- Add the parameters for the stored procedure here
    @table          SYSNAME,  --<-- use SYSNAME data type for Sql Server objects
    @date           date,
    @version        varchar(max),
    @process        varchar(max),
    @level          varchar(max),
    @message        varchar(max),
    @stacktrace     varchar(max),
    @user           varchar(max),
    @environmentID  varchar(max),
    @UUID           varchar(max),
    @UDID           varchar(max),
    @transactionID  int,
    @new_identity   int OUTPUT

AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @SQL NVARCHAR(MAX);

SET @SQL = N' INSERT INTO dbo.' + QUOTENAME(@Table)       --<-- use QUOTENAME function here (Sql Injection protection)
         + N'   ([Timestamp], [Version],Process,[Level],[Message]
                       ,StackTrace,[User],EnvironmentID,UUID,UDID,TransactionID)'
         + N' VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10) '
         + N' SELECT @new_identity =  SCOPE_IDENTITY(); ' --<-- Capture Identity value here

EXECUTE sp_executesql
      @SQL
    , N'@p0 DATE, @p1 VARCHAR(MAX), @p2 VARCHAR(MAX), @p3 VARCHAR(MAX)
        , @p4 VARCHAR(MAX), @p5 VARCHAR(MAX), @p6 VARCHAR(MAX), @p7 VARCHAR(MAX)
        , @p8 VARCHAR(MAX), @p9 VARCHAR(MAX), @p10 int, @new_identity INT OUTPUT'
    , @p0 = @date
    , @p1 = @version
    , @p2 = @process
    , @p3 = @level
    , @p4 = @message
    , @p5 = @stacktrace
    , @p6 = @user
    , @p7 = @environmentID
    , @p8 = @UUID
    , @p9 = @UDID
    , @p10 = @transactionID
    , @new_identity = @new_identity OUTPUT

END

Upvotes: 3

Shereen Bashar
Shereen Bashar

Reputation: 67

Make Sure That Parameters @new_identity In SQL Declare OutPut. Close Connection before set returnedID Consider Using @@IDENTITY

Upvotes: -1

Vladimir Semashkin
Vladimir Semashkin

Reputation: 1280

use the try-finally:

int returnedID = 0;
//.....
try
{
            cmd.ExecuteNonQuery();
}
finally
{
            int returnedID = Convert.ToInt32(cmd.Parameters["@new_identity"].Value); 
}

Upvotes: -1

Related Questions