0

I am trying to run the SQL Server procedure from C#:

try
{
    ExecWithStoredProcedure("storedprocName @Message, @ProviderCode", message, providecode);
}
catch(Exception ex)
{
}

public int ExecWithStoredProcedure(string query, params object[] parameters)
{
     return Context.Database.ExecuteSqlCommand(query, parameters);
} 

But I am getting this error:

Message handler encountered an exception
System.Data.SqlClient.SqlException (0x80131904): Cannot roll back ORU_Trans. No transaction or savepoint of that name was found.

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 2.

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 2.

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 2.

I have tried to run the procedure manually and its running without error in SSMS, but while trying in C# code I am getting this error.

7
  • other procedures executed by "Context.Database.ExecuteSqlCommand" work normally?
    – Diego Lobo
    Commented Feb 8, 2023 at 17:39
  • I only have this procedure called with in my solution with proper params. and inside my procedure I am calling other procs. point to note, when i run the procs from ssms it works
    – vin123
    Commented Feb 8, 2023 at 17:47
  • 2
    I would check first what is @@trancount in SSMS before and after running. Just in case the error is actually correct, and there's a mismatch in begin tran - commit pairs.
    – James Z
    Commented Feb 8, 2023 at 17:58
  • There are so many possibilities for why this is happening. Do you have triggers on any of the tables in these nested procedures? Do those triggers have an explicit commit or rollback? Do you have explicit transactions running across these procedures? It seems to me that you likely have some code patterns in your procedures that are masking the real issue. Why is that transaction attempting to be rolled back? Did some other error happen and you hid it? Without the code of your procedures (and maybe table definitions), this is too vague to answer.
    – Sean Lange
    Commented Feb 8, 2023 at 18:28
  • Even I thought the same and added TRY catch on each and every operation and tried to record the catched errors in a table. But nothing noticed. Strange thing is I am able to run this from SSMS by giving the inputs manually
    – vin123
    Commented Feb 8, 2023 at 18:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.