Skip to content Skip to sidebar Skip to footer

Getting Scope_identity From Sql Server On Insert

I guess it is too late and I'm too tired to see what I'm doing wrong. Here is what I'm trying: int imageId = imageDal.AddImage(new SqlParameter[] { new SqlParam

Solution 1:

I ended up with executescalar and returning the SCOPE_IDENTITY() directly from SP.

If there is another way of doing it and getting the value from the sqlparameter, I'd love to hear that out.

Solution 2:

I suppose, the problem is in ExecuteNonQuery method. It returns object array, instead of sqlparam's array.

Just have a ref on the output sqlparam object and get the value from that ref.

Good Luck!

P.S. There's another solution. Check the link

http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-distributed-apps/160/Data-Access-Application-Block-Output-Parameters

Solution 3:

You put Like this....

[dbo].[sp_insert_image]
    -- Add the parameters for the stored procedure here@IMAGE_ID intOUT,
    @IMAGE image
ASBEGININSERTINTO images (IMAGE) VALUES (@IMAGE)
    SET@IMAGE_ID  = SCOPE_IDENTITY();
END
GO

Post a Comment for "Getting Scope_identity From Sql Server On Insert"