I have this code in a method
string _errorMessage = string.Empty;
SqlParameter[] @params = {
new System.Data.SqlClient.SqlParameter("@id", entity.Id),
new System.Data.SqlClient.SqlParameter("@userId", _user.SecUserId),
new System.Data.SqlClient.SqlParameter("@lockProcess", "Bulk Receipting For Employer"),
new System.Data.SqlClient.SqlParameter("@errorMessage", SqlDbType.VarChar, 200) {Direction = ParameterDirection.Output}
};
var reader = _context.Database.ExecuteSqlCommand("usp_LockAndReadCase @id, @userId, @lockProcess, @errorMessage OUTPUT", @params);
_errorMessage = @params[3].Value.ToString();
return _errorMessage;
In one particular area, I call this method in a loop for where I have multiple Ids to pass. When it's called consecutively, I get the error:
System.InvalidOperationException: 'There is already an open DataReader associated with this Command which must be closed first.'
Since I am using Entity Framework and ASP.NET Core, I am using the ExecuteSqlCommand
and have no need to create a SqlConnection
or DataReader
.
How do I resolve this issue?