Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 222059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:05:30+00:00 2026-05-11T19:05:30+00:00

I have two stored procedures that I want execute wrapped in a transaction. For

  • 0

I have two stored procedures that I want execute wrapped in a transaction. For various reasons, I need to handle the transaction in my application code instead of within the database.

At the moment, my code looks like this:

try
{
    using (SqlConnection conn = Connection())
    {
        conn.Open();

        using (SqlTransaction sqlTrans = conn.BeginTransaction())
        {
            try
            {
                using (SqlCommand cmd1 = new SqlCommand("Stored_Proc_1", conn, sqlTrans))
                {
                    cmd1.CommandType = CommandType.StoredProcedure;
                    cmd1.ExecuteNonQuery();
                }

                using (SqlCommand cmd2 = new SqlCommand("Stored_Proc_2", conn, sqlTrans))
                {
                    cmd2.CommandType = CommandType.StoredProcedure;
                    cmd2.ExecuteNonQuery();
                }

                sqlTrans.Commit();
            }
            catch
            {
                    sqlTrans.Rollback();

                    throw;
            }

        }

        conn.Close();
    }
}

catch (SqlException ex)
{
  // exception handling and logging code here...
}

When one of the stored procs raises an error, the exception message I am seeing looks like:

Error message from raiserror within stored procedure.
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 0.

Which makes sense, because at the first catch, the transaction has not been rolled back yet.

But I want a “clean” error (without the tran count message – I’m not interested in this because I am rolling back the transaction) for my exception handling code.
Is there a way I can restructure my code to achieve this?

EDIT:

The basic structure of my stored procs looks like this:

create proc Stored_Proc_1
as

set nocount on

begin try
    begin transaction 

        raiserror('Error raised by Stored_Proc_1', 16, 1)       

    commit

end try
begin catch  
    if (@@trancount > 0) rollback   

    declare @ErrMsg nvarchar(4000), @ErrSeverity int, @ErrProc sysname, @ErrLine varchar(10)
    select @ErrMsg = ERROR_MESSAGE(), @ErrSeverity = ERROR_SEVERITY(), @ErrProc = ERROR_PROCEDURE(), @ErrLine = ERROR_LINE()

    -- log the error 
    -- sql logging code here...

    raiserror(@ErrMsg, @ErrSeverity, 1) 
end catch

UPDATE:
I’ve taken the transaction handling out of my stored procedures and that seems to have solved the problem. Obviously I was doing it wrong – but I’d still like to know how to do it right. Is removing transactions from the stored procedures the best solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:05:31+00:00Added an answer on May 11, 2026 at 7:05 pm

    Well, the conn.Close() could go anyway – it’ll get closed by the using (if you think about it, it is odd that we only Close() it after an exception).

    Do either of your stored procedures do any transaction code inside themselves (that isn’t being rolled back/committed)? It sounds like that is where the problem is…? If anything, the error message suggests to me that one of the stored procedures is doing a COMMIT even though it didn’t start a transaction – perhaps due to the (incorrect) approach:

    -- pseduo-TSQL
    IF @@TRANCOUNT = 0 BEGIN TRAN
    -- ...
    IF @@TRANCOUNT > 0 COMMIT TRAN -- or maybe = 1
    

    (if you do conditional transactions in TSQL, you should track (via a bool flag) whether you created the transaction – and only COMMIT if you did)

    The other option is to use TransactionScope – easier to use (you don’t need to set it against each command etc), but slightly less efficient:

    using(TransactionScope tran = new TransactionScope()) {
        // create command, exec sp1, exec sp2 - without mentioning "tran" or
        // anything else transaction related
    
        tran.Complete();
    }
    

    (note there is no rollback etc; the Dispose() (via using) will do the rollback if it needs to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How's about this for a succinct description: Procedural Programming is… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer One constructor for shared_ptr takes the destructor method, and you… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer There are a lot of applications for which a cluster… May 12, 2026 at 1:04 am

Related Questions

I have a couple of scenarios: Need to read the value of a column
I have two stored procedures I wish to use in my stored procedure, but
I am using nested repeaters to build a table for reasons I won't discuss
Just dipping my toes into Linq2sql project after years of rolling my own SQL

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.