I want to assign the parameter @ResponseMessage to the C# variable responseMessage. I want to use Postman to set the values of the other parameters.
I managed to get the JSON to return the response message to return NULL but never return the what I need to be.
I have the following SQL Server stored procedure:
ALTER PROCEDURE [dbo].[Register]
(@FirstName NVARCHAR(30),
@LastName NVARCHAR(30),
@Email NVARCHAR(30),
@Password NVARCHAR(30),
@ResponseMessage NVARCHAR(30) OUTPUT)
AS
BEGIN TRANSACTION
BEGIN
IF NOT EXISTS (SELECT 1 FROM Users WHERE Email = @Email)
BEGIN
BEGIN TRY
DECLARE @i INT;
INSERT INTO Users (FirstName, LastName, Email, Password)
VALUES (@FirstName, @LastName, @Email, @Password)
SELECT @i = SCOPE_IDENTITY();
SET @ResponseMessage = CONCAT(200, @i)
COMMIT;
END TRY
BEGIN CATCH
SET @ResponseMessage = 404
ROLLBACK;
END CATCH
END
ELSE
BEGIN
SET @ResponseMessage = 208
COMMIT;
END
END
This is my C# code that I want to save the parameter to
public void Register(SqlParameter[] parameters, out string responseMessage)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 30);
cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 30);
cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 30);
cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 30);
cmd.Parameters.Add("@ResponseMessage", SqlDbType.NVarChar, 30).Direction = ParameterDirection.Output;
responseMessage = (string)cmd.Parameters["@ResponseMessage"].Value;
Database.ExecuteSqlRaw("EXEC Register @FirstName, @LastName, @Email,
@Password, @ResponseMessage", parameters);
}
[HttpPost]
public IActionResult registerUser (User newUser)
{
Register newReg = new Register();
SqlParameter[] parameters =
{
new SqlParameter("@FirstName", newUser.FirstName.ToString()),
new SqlParameter("@LastName", newUser.LastName.ToString()),
new SqlParameter("@Email", newUser.Email.ToString()),
new SqlParameter("@Password", newUser.Password.ToString())
};
_context.Register(parameters, out string responseMessage);
Dictionary<string, string> keys = new Dictionary<string, string>();
keys.Add("User ID ", responseMessage);
JsonResult result = new JsonResult(keys);
return Ok(result);
}
This shows my Postman screen and the values that I want to be entered. It does add to the database but the return message does not display what i want it to.