Stored procedure works and deletes what I want but I still get this error after deleting:
The data reader is incompatible with the specified 'AMSIdentity.Models.RemoveRoleFromUserViewModel'. A member of the type, 'RoleId', does not have a corresponding column in the data reader with the same name.
I need to run the code without this error in the above
This code using ASP.NET MVC 5 and EF6 code first approach; I tried to use this code but always throws this error after delete.
This is the action method that I use
public ActionResult RemoveRoleFromUserConfirmed(string UserName, string RoleId)
{
if (UserName == null && RoleId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SqlParameter param1 = new SqlParameter("@RoleId", RoleId);
SqlParameter param2= new SqlParameter("@UserName", UserName);
var remove = Identitydb.Database.SqlQuery<RemoveRoleFromUserViewModel>("admin.sp_RemoveUserFromRole @RoleId, @UserName",
((ICloneable)param1).Clone(),
((ICloneable)param2).Clone()).ToArray().ToList().FirstOrDefault();
if (remove == null)
{
return HttpNotFound();
}
return RedirectToAction("Roles");
}
This is the view model that I use :
public class RemoveRoleFromUserViewModel
{
[Key]
[DisplayName("Role Id")]
public string RoleId { get; set; }
[DisplayName("Username")]
public string UserName { get; set; }
}
This is the stored procedure code:
ALTER PROCEDURE [Admin].[sp_RemoveUserFromRole]
@RoleId NVARCHAR(50),
@UserName NVARCHAR(50)
AS
BEGIN
DELETE FROM AspNetUserRoles
WHERE UserId = (SELECT Id
FROM AspNetUsers
WHERE UserName = @UserName)
AND RoleId = @RoleId
END
I expect that this code will delete role from the specific user.
Hi All,
I got the answer from @Jeroen Mostert, The solution is to use the (ExecuteSqlCommand) rather than (SqlQuery) because I will never return data, I only execute the stored procedure with two parameters.
SqlParameter param1 = new SqlParameter("@RoleId", RoleId);
SqlParameter param2= new SqlParameter("@UserName", UserName);
//I change this line from SqlQuery to ExecuteSqlCommand
var remove = Identitydb.Database.ExecuteSqlCommand("admin.sp_RemoveUserFromRole @RoleId, @UserName", param1, param2);
Thank you very much @Jeroen Mostert.
Regards,
Ali Mosaad
Software Developer