I am working CRUD app in Blazor and I got an error when doing the delete user function. I tried many ways but failed. Here is the code in my 3 files.
DAO.cs
public void DeleteUser(List<int> userIds)
{
string deleteQuery = "DELETE FROM Users WHERE UserID IN (@userIds)";
Dictionary<string, object> delParams = new Dictionary<string, object> {
{ "@userIds", userIds }
};
sqlManager.ExecuteNonQuery(deleteQuery, delParams);
}
Service.cs
public void DeleteUsers(List<int> userIds)
{
dao.DeleteUser(userIds);
}
User.razor
private List<Users> selectedUsers = new List<Users>();
private async Task DeleteUser()
{
List<int> userIds = selectedUsers.Select(u => u.UserID).ToList();
service.DeleteUsers(userIds);
NavigationManager.NavigateTo("/User", forceLoad: true);
}
I cannot delete user and this error:
How to fix this error
Try to modify this
In Service.cs
with this change, you pass the userIds list to the DAO layer. You need to pass it as a parameter array rather than a list.
In DAO.cs:
You have to change the way how do the sql query for allow multiple params
In the method private async Task DeleteUser() change how pass the param, Convert toList