Additional information: Incorrect syntax near @UserPic.
What is causing this, and how can I resolve it?
Controller code:
[HttpPut("{id}")]
public ActionResult<User> ChangeUser(int id, User userToUpdate)
{
User user = dataRepository.GetUser(id);
if (user == null) { return NotFound(); }
var updatedUser = dataRepository.UpdateUser(id, userToUpdate);
return updatedUser;
}
Data repository code:
public User UpdateUser(int id, User user)
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
connection.Execute(@"EXEC dbo.updateUser @UserId = @UserId @UserPic = @UserPic @Name = @Name @Password = @Password",
new { UserId = id, UserPic = user.UserPic, Name = user.Name, Password = user.Password });
return GetUser(id);
}
}
Sql code:
CREATE PROC dbo.updateUser
(
@UserId int,
@UserPic nvarchar(max),
@Name nvarchar(150),
@Password nvarchar(150))AS BEGIN
SET NOCOUNT ON
UPDATE [dbo].[User]
SET [UserPic] = @Userpic, [Name] = @Name, [Password] = @Password
WHERE [UserId] = @UserId
SELECT * FROM [dbo].[User] WHERE [UserId]=@UserId END GO
JSON data to update:
{"UserPic":"href","Name":"Maria","Password":"idj333"}
User model:
public class User
{
public int UserId { get; set; }
public string UserPic { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public DateTime Registered { get; set; }
public string Type { get; set; }
}