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 8446441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:56:06+00:00 2026-06-10T09:56:06+00:00

I am writing code in C# that makes a call to two separate stored

  • 0

I am writing code in C# that makes a call to two separate stored procedures. The first procedure does not always get called, however when it does it needs to complete it’s task before the second can begin. What’s happening is the two seem to be running side by side instead. The first task is supposed to update a number used as an identifier in the second task. The second task, however, if it does not find an identifier will add a record instead or performing updates to it. Since I keep finding both the appropriate record updated AND a new record inserted, I’m wondering if either A: I’m writing this wrong or B: is it possible to have stored procedures called and completed sequentially in a transaction?

try
{
    cm = Dts.Connections["serverName"];
    sqlConn = (SqlConnection)cm.AcquireConnection(Dts.Transaction);
    sqlTrans = sqlConn.BeginTransaction("QueueUpdates");

    if (dummyIndicator.Equals("Y"))
    {
        string temp = retrievedMessage.Substring(203, 17);
        int oldNumber = (int)(long.Parse(temp) / 777);

        SqlParameter newNum = new SqlParameter("@newNum", num.Value);
        SqlParameter oldNum = new SqlParameter("@oldNum", oldNumber);

        sqlComm2 = new SqlCommand("DB.dbo.sp_UpdateNum", sqlConn, sqlTrans);
        sqlComm2.CommandType = CommandType.StoredProcedure;
        sqlComm2.Parameters.Add(newNum);
        sqlComm2.Parameters.Add(oldNum);
        sqlComm2.Transaction = sqlTrans;
        sqlComm2.ExecuteNonQuery();
    }

    //Update records according to queue messages
    sqlComm = new SqlCommand("DB.dbo.sp_NumCheck", sqlConn, sqlTrans);
    sqlComm.CommandType = CommandType.StoredProcedure;
    sqlComm.Parameters.Add(num);
    sqlComm.Parameters.Add(addOrUpdate);
    sqlComm.Parameters.Add(companyCode);
    sqlComm.Parameters.Add(agentID);
    sqlComm.Parameters.Add(firstName);
    sqlComm.Parameters.Add(lastName);
    sqlComm.Parameters.Add(suffix);
    sqlComm.Parameters.Add(taxIdType);
    sqlComm.Parameters.Add(entityType);
    sqlComm.Parameters.Add(corporateName);
    sqlComm.Parameters.Add(outNewNumber);
    sqlComm.Parameters.Add(outCurrentNumber);
    sqlComm.Parameters.Add(outOperator);
    sqlComm.Parameters.Add(outDate);
    sqlComm.Parameters.Add(returnVal);
    sqlComm.ExecuteNonQuery();

    sqlTrans.Commit();

    if (addOrUpd.Equals("ADD")){recordsAdded++;}
    else{recordsUpdated++;}

}
catch (Exception ex)
{
    _sqlDataErrors++;
    swLog.WriteLine("Message not updated: " + retrievedMessage);
    swLog.WriteLine("Error: " + ex.ToString());
    sqlTrans.Rollback();
}
finally
{                                
    cm.ReleaseConnection(sqlConn);
}

Here is some of the code from the stored procedures:

sp_UpdateNum – When called, this needs to complete first

ALTER PROCEDURE [dbo].[sp_UpdateNum]
        (
        @newNum char(9),
        @oldNum char(9)
        )

AS

UPDATE AgentIdentification
SET AgentId = @newNum
WHERE AgentId = @oldNum

sp_NumCheck – There is a lot of code in this one but here is the most relevant part: These are the first lines that get executed in this stored procedure. It runs a check on the AgentId. If an instance of the specific ID is found then it will perform an update. If not, it will perform an insert. What’s happening when the two get called at the same time is the ID number is updated, then a new record is inserted. I’ve checked to make sure all variable values are correct as well.

SELECT @rows = COUNT(AgentId) 
FROM AgentIdentification
WHERE AgentId = @num

IF @rows > 0

EDIT

This wound up being a problem with the logic in the second stored procedure. The above listed method for creating two SQL transaction via C# code was done properly.

  • 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-10T09:56:08+00:00Added an answer on June 10, 2026 at 9:56 am

    I think you don’t need to do the transaction in that way, you can use the TransactionScope and it is easier to use and Disposable also.

    Basically is what this link refers

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

Sidebar

Related Questions

I'm writing a simple Javascript library that makes use of some WebGL code. I'd
I am writing code that will spawn two thread and then wait for them
I've had trouble writing this code. I'm supposed to make a function that can
I am writing code that catches this OutOfMemoryException and throws a new, more intuitive
I'm using c#. In many cases I'm writing code that can benefit from a
I am a very proficient C# developer, but need to start writing code that
I'm writing some code that will take a screenshot of another application, given its'
I am writing some code that will go through a file, edit it as
I'm writing a code that uses bitboards. since iterating on all bits of a
I'm writing some code that has a lot of substitution. There's a list<char> productions

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.