title: Uncommittable transaction is detected at the end of the batch. The transaction is rolled back
body: <p>I have a stored procedure, that I'm calling from a c# code using a transaction.
  When I run the code in C# (its a console app), instead of getting the result from catch block I'm getting an exception thrown saying:</p>
  
  <blockquote>
    <p>Uncommittable transaction is detected at the end of the batch. The
    transaction is rolled back.</p>
  </blockquote>
  
  <p>C# code:</p>
  
  <pre><code>using System;
  using System.Configuration;
  using System.Data;
  using System.Data.SqlClient;
  
  namespace app1
  {
      class Program
      {
          static void Main(string[] args)
          {
              string res = "";
              string resDesc = "";
  
              res = WriteToDB(1,out resDesc);
  
              Console.WriteLine(res);
              Console.WriteLine(resDesc);
  
  
              Console.Read();
          }
  
          public static string WriteToDB(int val, out string resultDesc)
          {
              resultDesc = "";
              string result = "";
              string connectionString = ConfigurationManager.ConnectionStrings["SqlAppConnection"].ConnectionString;
              using (SqlConnection connection = new SqlConnection(connectionString))
              {
                  connection.Open();
                  SqlTransaction transaction = connection.BeginTransaction("transcation1");
                  try
                  {
                      using (SqlCommand cmd = new SqlCommand())
                      {
                          cmd.Connection = transaction.Connection;
                          cmd.Transaction = transaction;
                          cmd.CommandText = "usp_Test_Proc";
                          cmd.CommandType = CommandType.StoredProcedure;
                          cmd.Parameters.AddWithValue("@check", val);
                          SqlParameter res = cmd.Parameters.Add("@result", SqlDbType.Int);
                          res.Direction = ParameterDirection.Output;
                          SqlParameter resDesc = cmd.Parameters.Add("@resultDesc", SqlDbType.VarChar, 100);
                          resDesc.Direction = ParameterDirection.Output;
                          cmd.ExecuteNonQuery();
                          result = res.Value.ToString().Trim();
                          resultDesc = resDesc.Value.ToString();
                          transaction.Commit();
                      }
                  }
                  catch (Exception ex)
                  {
                      result = "Exception";
                      resultDesc = ex.Message;
                      transaction.Rollback();
                  }
              }
              return result;
          }
  
      }
  }
  </code></pre>
  
  <p>Stored procedure:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[usp_Test_Proc] (
                                              @check        int, 
                                              @result           INT output, 
                                              @resultDesc       VARCHAR(100) 
  output) 
  AS 
    BEGIN 
        SET nocount ON; 
        SET xact_abort ON;             
  
        IF @check != 0 
          BEGIN 
              BEGIN try              
                  SET @result = 0; 
                  SET @resultDesc = 'aa'; 
                  --RAISERROR('Error from raiserror',1,1)
                  THROW 99001, 'Error from throw', 1;
              END try
              BEGIN catch 
                  SET @result = 1; 
                  SET @resultDesc = concat('catch block',ERROR_MESSAGE()); 
              END catch; 
          END 
          ELSE
          BEGIN
                  SET @result = 0; 
                  SET @resultDesc = 'done'; 
                  end
    END; 
  
  GO
  </code></pre>
  
  <p>When the stored procedure throws an error, instead of going in to the catch block I'm getting an exception back saying "Uncommittable transaction is detected at the end of the batch. The transaction is rolled back."</p>
  
  <p>But if I run the stored procedure in SSMS it works as expected:
  <a href="https://i.stack.imgur.com/08TeK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/08TeK.png" alt="enter image description here"></a></p>
  
  <p>Why is the result different when I'm calling it from C# code?</p>