I've tried two different thoughts in resolving this issue without success. I've look at the following links here, here and here but I end up with
Object reference not set to an instance of an object.
or
Procedure or function InsertHero has too many arguments specified.
When I execute the stored procedure in SSMS, my IDs also appear off in that they are incrementing from 1110 instead of the original number set at 100 (this might be a different issue).
It seems like I'm 99% of the way there and I'm missing a small piece. What am I missing?
C#:
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("InsertHero", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Name", hero.Name);
command.Parameters.AddWithValue("@Universe", hero.Universe);
// command.Parameters.Add("@Created_Id", SqlDbType.Int).Direction = ParameterDirection.Output;
int id = (int)command.ExecuteScalar();
}
}
Stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertHero]
@Name nvarchar(50),
@Universe nvarchar(15),
@Created_Id int OUTPUT
AS
INSERT INTO Hero
VALUES (@Name, @Universe);
SET @Created_Id = SCOPE_IDENTITY()
GO
Stored procedure (different way):
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertHero]
@Name nvarchar(50),
@Universe nvarchar(15) OUTPUT
AS
INSERT INTO Hero
OUTPUT inserted.Id
VALUES (@Name, @Universe);
GO
EDIT:
Leaving this here in case someone stumbles upon it. Turned out to be a problem with the connection string so be sure to double and maybe triple check