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

  • SEARCH
  • Home
  • 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 8394161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:59:07+00:00 2026-06-09T19:59:07+00:00

Below is the code I’m using in an SSIS script task. I am trying

  • 0

Below is the code I’m using in an SSIS script task. I am trying to make both inserts atomic as they deal with similar customers.

The first .executeNonQuery() works fine, locking the SQL table as it should.

The second .executNonQuery() throws an error:

ExecuteNonQuery requires the command to have a transaction when the
connection assigned to the command is in a pending local transaction.
The Transaction property of the command has not been initialized.

Code:

    ConnectionManager cm; 
    SqlTransaction sqlTrans;
    SqlConnection sqlConn;
    SqlCommand sqlComm;                                  
    cm = Dts.Connections["connectionManager"];

    try
    {
         //Set 'global' variables                                        
         SqlParameter agentID = new SqlParameter("@agentID", 1000018); //retrievedMessage.Substring(2, 10));//Primary key
         SqlParameter lastChangeOperator = new SqlParameter("@lastChangeOperator", "LVO");
         SqlParameter lastChangeDate = new SqlParameter("@lastChangeDate", DateTime.Now);
         SqlParameter controlId = new SqlParameter("@controlID", 1); //Hard-coded value for testing - CHANGE LATER

         //Set variables for Agent table
         SqlParameter entityType = new SqlParameter("@entityType", "P");//retrievedMessage.Substring(162, 1));
         SqlParameter fName = new SqlParameter("@fName", "test");//retrievedMessage.Substring(12, 25));
         SqlParameter lName = new SqlParameter("@lName", "test");//retrievedMessage.Substring(37, 35));
         SqlParameter suffix = new SqlParameter("@suffix", "test");//retrievedMessage.Substring(72, 10));
         SqlParameter corporateName = new SqlParameter("@corporateName", "Initech");//retrievedMessage.Substring(82, 80));

         //Insert record into Agent table
         sqlConn = (SqlConnection)cm.AcquireConnection(Dts.Transaction);
         sqlComm = new SqlCommand
         (
           "SET IDENTITY_INSERT Agent ON " +
           "INSERT INTO Agent (UniqueAgentId, EntityType, FirstName, LastName, NameSuffix, CorporateName, LastChangeOperator, LastChangeDate, ControlId) " +
           "VALUES (@agentID, @entityType, @fName, @lName, @suffix, @corporateName, @lastChangeOperator, @lastChangeDate, @controlID)" +
           "SET IDENTITY_INSERT Agent OFF",
            sqlConn//, sqlTrans
         );

         sqlTrans = sqlConn.BeginTransaction("SqlAgentTableUpdates");
         sqlComm.Parameters.Add(agentID);
         sqlComm.Parameters.Add(lastChangeOperator);
         sqlComm.Parameters.Add(lastChangeDate);
         sqlComm.Parameters.Add(controlId);
         sqlComm.Parameters.Add(entityType);
         sqlComm.Parameters.Add(fName);
         sqlComm.Parameters.Add(lName);
         sqlComm.Parameters.Add(suffix);
         sqlComm.Parameters.Add(corporateName);
         sqlComm.Transaction = sqlTrans;
         sqlComm.ExecuteNonQuery();

         //Set variables for AgentIdentification table
         SqlParameter taxIdType = new SqlParameter("taxIdType", "S");//Hard-coded value for testing - CHANGE LATER
         SqlParameter agentTaxId = new SqlParameter("@agentTaxId", "999999999");//Hard-coded value for testing - CHANGE LATER

         //Insert record into AgentIdentification table
         sqlConn = (SqlConnection)cm.AcquireConnection(Dts.Transaction);
         sqlComm = new SqlCommand
         (
           "INSERT INTO AgentIdentification (UniqueAgentId, TaxIdType, AgentTaxId, LastChangeOperator, LastChangeDate, ControlId) " +
           "VALUES (@agentID, @taxIdType, @agentTaxId, @lastChangeOperator, @lastChangeDate, @controlId)",
            sqlConn//, sqlTrans
         );

         sqlComm.Parameters.Add(taxIdType);
         sqlComm.Parameters.Add(agentTaxId);
         sqlComm.Transaction = sqlTrans;
         sqlComm.ExecuteNonQuery();
    }
    catch (Exception)
    {
       sqlTrans.Rollback();
       cm.ReleaseConnection(sqlConn);
    }
    finally
    {
       sqlTrans.Commit();
       cm.ReleaseConnection(sqlConn);
    }

EDIT

I was able to make this transaction work by eliminating the second connection. However, both queries use a couple of the same variables (SqlParameters). I was forced to duplicate these in order for this to run without errors. Is there a way for them to share the variables so I do not have to waste space re-creating them?

  • 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-06-09T19:59:08+00:00Added an answer on June 9, 2026 at 7:59 pm

    I think the problem might be with the connection, or when you set the command to a new command for the second insert, you can use two different commands with the same connection or reuse one command just changing the CommandText property.

    Hope this helps… Using SqlTransaction

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

Sidebar

Related Questions

The below code is in my javascript/jquery script, but for some reason Firebug tells
Below code i am using for MSSQL 2005,but when i try to run in
Below code work only for latin n , but how to make it work
Below is my code and I have been trying to color the cells but
Below code not work, but it's work fine for jsf1.2. Now the framework is
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
below code is my databasehandler class i got it from a tutorial. Beside that
Below code : URL oracle = new URL(http://www.oracle.com/); URLConnection inputStream =oracle.openConnection(); InputStream in =
Below code makes form to be submitted without html5 validation... $j(document).on(click, #client_entry_add, function(event){ ajax_submit();});
In below code when I click 'Vote' a vote results screen is displayed but

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.