I am trying to use stored procedures in Entity Framework Core. When executing a stored procedure, I am passing two input parameters and one output parameter. I keep getting this error:
The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.
This is my code:
public string InsertCardsData(DateTime RecordingStartDate, DateTime RecordingEndDate)
{
try
{
// DateTime DRecEndDate = Convert.ToDateTime(RecordingEndDate);
DateTime DRecEndDate = RecordingEndDate.AddDays(1);
var RecordStartDate = new SqlParameter
{
ParameterName = "RecordingStartDate",
Value = RecordingStartDate,
Direction = ParameterDirection.Input
};
var RecordEndDate = new SqlParameter
{
ParameterName = "RecordingEndDate",
Value = DRecEndDate,
Direction = ParameterDirection.Input
};
var RecordCount = new SqlParameter
{
ParameterName = "RecLoadCount",
Direction = ParameterDirection.Output
};
var SQL = "Exec Recload_InsertPrimeExtract @RecordingStartDate, @RecordingEndDate, @RecLoadCount OUT";
var result = _context.Database.ExecuteSqlRaw(SQL, RecordStartDate, RecordEndDate, RecordCount);
var RecordCountValue = RecordCount.Value.ToString();
return RecordCountValue;
}
catch (Exception ex)
{
return "";
}
}
I will put a a meaningful catch statement, but right now, I am putting a breakpoints in catch statement and the above error occurs.
Any help will be highly appreciated.
DateTime DRecEndDate = Convert.ToDateTime(RecordingEndDate)you are converting a datetime to a datetime ? Also why are you returning a string for a count which is obviously numericusing System.Data.SqlClientorusing Microsoft.Data.SqlClient;?