I cannot delete a user

69 views Asked by At

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:

enter image description here

How to fix this error

1

There are 1 answers

0
Deivi Robles On BEST ANSWER

Try to modify this

In Service.cs

public void DeleteUsers(params int[] userIds)
{
    dao.DeleteUser(userIds);
}

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:

public void DeleteUser(params int[] userIds)
{
    string deleteQuery = "DELETE FROM Users WHERE UserID IN (" + string.Join(",", userIds) + ")";
    sqlManager.ExecuteNonQuery(deleteQuery);
}

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

service.DeleteUsers(userIds.ToArray())