1

I'm using .Net EF Core 3.1.5, I need to call an existing SQL Server stored procedure with parameters.

The procedure is executing fine and the parameter I pass in are working.

I'm having an issue whereby no warning or errors are being caught from SQL Server. My stored procedure is 'GetProcessEvents' which takes parameter @runId. If I change @runId to be @xxxxxId in my C# the stored procedure will not run but no error will be returned to my C#.

I've tried return Convert.ToInt32(cmd.ExecuteScalar()); and as in the code below 'ExecuteReader' but neither work. Even if I put a 'select 100/0' in the stored procedure no exception is returned - If I run the procedure directly in SQL Server Management Studio I get "Msg 8134, Level 16, State 1, Line 1. Divide by zero error encountered." which I would expect as an exception in C#.

try
{
    if (sqlConn == null)
        return 0;

    DbCommand cmd = _context.Database.GetDbConnection().CreateCommand();

    cmd.CommandText = "GetProcessEvents";
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("@runId", runId));

    if (cmd.Connection.State != ConnectionState.Open)
        cmd.Connection.Open();

    var reader = cmd.ExecuteReader();

    if (reader.HasRows) // I've tried with and without this if
    {
        while (reader.Read())
            return reader.GetInt32(0);
    }
    return runId;
}
catch (SqlException sqle)
{
    throw sqle; // No exception ever caught
}
catch (Exception e)
{
    throw e;
}

Do I need to try one of the other solutions in This StackOverflow - Although I tried FromSql and Visual Studio indicates it's obsolete.

Please help. Many thanks!

Also, In case this adds anything, I'm using SignalR successfully to retrieve SQL Server warning (< less than severity 10 - so not error) events (SQL: RAISERROR ( '1' ,0, 10) WITH NOWAIT) raised from the stored procedure which fires back progress events as the possibly long running procedure is executing. This is working fine and the events are being received by C# from SQL Server. Please Note: I have tried the above issue with and without wiring up the code below.

sqlConn.FireInfoMessageEventOnUserErrors = true;

// Number of times the stored procedure(s) will return notifications to the Client
if (percentageNotifications == null)
    percentageNotifications = 10;

// Wire up delegate to SQL Server connection
sqlConn.InfoMessage += delegate (object sender, SqlInfoMessageEventArgs e)
{
    // Fire notification to SignalR Hub and anwards to client on RAISERROR (severity <10: just a message) from Sql Server
    NotificationsComplete++;
    _contextHub.Clients.All.SendAsync("UpdateProgressBar", taskId, ((100 / percentageNotifications) * NotificationsComplete).ToString());
};
 

  
Thomas Byrne
  • 127
  • 1
  • 2
  • 15
  • We do not see the declaration of "runId" to check whether it's the right type. Also, maybe try the parameters.AddWithValue overload – Romka Aug 21 '20 at 17:26
  • Perhaps you need to consume all result sets and messages. See [this article](https://www.dbdelta.com/the-curious-case-of-undetected-sql-exceptions/). – Dan Guzman Aug 21 '20 at 19:13

0 Answers0