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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:30:49+00:00 2026-05-26T17:30:49+00:00

Hi all i have written the following transaction to insert data but when i

  • 0

Hi all i have written the following transaction to insert data but when i am getting an exception only the data which got the exception not inserting to db the remaining all are inserting

This is what i wrote

public bool addWhole(SqlTransaction osqlTrans)
{
   m_flag = false;
   osqlTrans = null;

    SqlConnection osqlCon = new SqlConnection(constr);

    if (osqlCon.State != ConnectionState.Open)
    {
        osqlCon.Open();
    }

    osqlTrans = osqlCon.BeginTransaction();

    try
    {
      if (this.addRisk(osqlTrans, osqlCon))
      {
       if (this.addEconomical(osqlTrans, osqlCon))
       {
           osqlTrans.Commit();
       }
      }
     }
    catch (Exception ex)
    {
        osqlTrans.Rollback();
    }
    finally
    {
        osqlCon.Close();
    }
    return m_flag;
}

public bool addRisk(SqlTransaction oRiskTrans, SqlConnection oRiskConn)
{
     con = new SqlConnection(constr);
    if (con.State != ConnectionState.Open)
    {
        con.Open();
    }
    cmd = new SqlCommand("insert into tblEnrollmentData (EID,Eyear,Epercent) values('" + id + "','" + str + "','" + dbPercent + "')", con); //Even i tried adding transaction in command statement
            if (cmd.ExecuteNonQuery() > 0)
            {
                m_flag = true;
            }
   }

public bool addEconomical(SqlTransaction oRiskTrans, SqlConnection oRiskConn)
{
     con = new SqlConnection(constr);
    if (con.State != ConnectionState.Open)
    {
        con.Open();
    }
    cmd = new SqlCommand("insert into tblEnrollmentData (EID,Eyear,Epercent) values('" + id + "','" + str + "','" + dbPercent + "')", con);//Even i tried adding transaction in command statement
            if (cmd.ExecuteNonQuery() > 0)
            {
                m_flag = true;
            }
   }

I tried to rollback the transaction by failing the second condition but my first statement is inserting to DB.. What should i do in order to overcome this

  • 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-26T17:30:49+00:00Added an answer on May 26, 2026 at 5:30 pm

    I’m guessing its one of these things

    1) You are not using the same connection object for all the different commands
    2) You are not assigning the transaction to the commands before you execute them
    3) Both perhaps

    Try using the same connection object and assigning the transaction to the command before you execute it for an example see this page on MSDN. http://msdn.microsoft.com/en-us/library/86773566.aspx

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

Sidebar

Related Questions

I have written following function which checks whether start_date field is not empty and
I have written following lines to click all links in fetched page but it
I have written the following function: -- Gets stats for all markets CREATE OR
I have written the following function which adds an event listener and registers the
I have written the following code snippet to read data from the database :
I have written the following code to find out the missing sequence but i
I have written the following piece of code, all i want to do is
I have written the following code and not able to figure out the errors
I have written the following query to return all the leaves that an employee
I am a newbie in Python. I have written following program and expecting all

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.