I have a stored procedure that I am calling using the ObjectContext
ExecuteStoreQuery
method so that I can get the result set that the stored procedure returns.
I have it inside of a try/catch block, however even though the stored procedure returns the following error it doesn't fall into the catch block.
How do I trap for this type of error?
Or is there another way of executing a stored procedure and getting a result set back?
Error when executed in SQL:
Msg 50000, Level 16, State 2, Procedure ComputeCharges, Line 6440
The following error occurred while computing charges:
Error Number: 515, Line Number: 5867
Error Message: Cannot insert the value NULL into column 'TransactionAmount', table 'Transactions'; column does not allow nulls. INSERT fails.
Calling code:
using (DbContext dbContext = GetDbContext())
{
using (ObjectContext objContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dbContext).ObjectContext)
{
try
{
IEnumerable<ResultSet> results = objContext.ExecuteStoreQuery<ResultSet>(sqlString).ToList();
}
catch (Exception e)
{
EventLogger.LogException(e, title: "An error occurred while computing charges.", silent: false);
}
}
}
An exception will be thrown in C# correctly in this case as long as the result set has yet to be selected/returned. The stored procedure was selecting the result set in the middle of the process and not at the end of the try block. Once the select statement for the result set was moved to the end of the try block it threw the exception in C#.