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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:25:46+00:00 2026-06-09T03:25:46+00:00

I’m not including any code here because I’ve re-written it so many times over

  • 0

I’m not including any code here because I’ve re-written it so many times over the past few days its really not worth bothering with any more, so I’ll explain my problem in generic terms.

I have a winforms application written in C# and connected to a sql server database. In certain parts of my application I can have as many as 10 SQL command operating on numerous tables within the database, and which I need to contain within a single transaction, because it is an ‘all or nothing’ situation. If any one of the 10 commands fails, I want non of the commands to be committed.

Complicating factors include:

  1. Each of the 10 sqlcommands have multiple parameters
  2. Some of the sqlcommands use INSERT and I need to use the server generated identity in some of the subsequent sqlcommands.
  3. Some of the commands operate on the same data i.e. an early command might INSERT a new item in the database and one of the later sqlcommands will attempt to update that same entry.

Any help would be most appreciated as I am at my wits end with this issue having spent most of the week on it and feeling like relatively little progress has been made.

EDIT:

The sqlcommands were originally in different classes etc, however I though that might be the problem so I did a big re-write and now ALL of the sqlcommands are within the same method, albeit some of the sqlcommands are written elsewhere and passed back to the main method.

The problem at the moment is that, the first command (INSERT) is executed, but the transaction fails at the second command because that one attempts to refer to the database entry created in the first command.

Also I cannot use TransactionScope because MSDTC on the network causes other issues and so I need to handle this with my application and ADO.NET.

EDIT (SOME CODE)

Here is just the first two sqlcommands within the transaction:

string connectionString = aSystem.ConnectionString;

    using (SqlConnection linkToDB = new SqlConnection(connectionString))
    {
        linkToDB.Open();
        using (SqlTransaction transaction = linkToDB.BeginTransaction())
        {
            try
            {                            
                //...Case...//

                cCase c = new cCase();
                c.CaseType = cboCaseType.Text;
                c.Occupation = txtOccupation.Text;
                c.DateInstructed = txtDateInst.Text;
                c.Status = "Live";
                SqlCommand sqlSaveCase = c.SaveSqlCom();                               
                sqlSaveCase.Connection = linkToDB;
                sqlSaveCase.Transaction = transaction;
                c.SetCaseNo = sqlSaveCase.ExecuteNonQuery().ToString();

                //...IP Link...//

                string strIPID = cboIPAddress.SelectedValue.ToString();
                SqlCommand sqlNewIPLink = cIPID.NewIPLinkSqlCom(strIPID,c.CaseNo,txtIPRef.Text);
                sqlNewIPLink.Connection = linkToDB;
                sqlNewIPLink.Transaction = transaction;
                sqlNewIPLink.ExecuteNonQuery();                //...fails here...//


                //...an additional 8 sql commands follow...//

ISSUE

The sqlSaveCase command INSERTS a new case on tblCases, but when the second command sqlNewIPLink attempts to use the Identity created by the first, it generates a Foreign Key error as if it does not see the newly created Case from the first command.

  • 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-09T03:25:48+00:00Added an answer on June 9, 2026 at 3:25 am

    The error was this line

    c.SetCaseNo = sqlSaveCase.ExecuteNonQuery().ToString(); 
    

    which should be using ExecutreScalar() in order to retrieve the ID from the INSERT command. The clue came when I eventually realised it was always returning 1, which I assume is probably just a boolean ‘true’ to say the command was executed. Once I used the ExecuteScalar on the command instead, it started to returning more meaningful IDs which subsequent SqlCommands did recognise and hence no Foregin Key issues.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.