TransactionScope - The operation is not valid for the state of thetransaction.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Svein Terje Gaup

    TransactionScope - The operation is not valid for the state of thetransaction.

    Hi, I have some code on .NET 2.0 that creates a TransactionScop e and
    then tries to open a SQL Connection.

    When I try to open the connection, I get this error:
    Test method
    CDNDWUnitTestsN et.CDNDWFacades Net_DealerAdmin Test.DeleteDeal erTest
    threw exception: System.Transact ions.Transactio nException: The
    operation is not valid for the state of the transaction..

    Everything is running on the same machine, using Windows XP SP2, SQL
    Server 2005.

    When the code is deployed, the database will be on a separate single
    server, so I think I will need to use distributed transactions.

    My code is like this:

    public int Save(string dealerXML)
    {
    using (TransactionSco pe scope = new
    TransactionScop e(TransactionSc opeOption.Requi red))
    {

    TransactionInte rop.GetTransmit terPropagationT oken(Transactio n.Current);
    this.SaveDealer Category(dealer .Category); //this uses
    a connection
    this.SaveDealer Chain(dealer.Ch ain); //this uses a
    connection
    cnn = new SqlConnection(c onnectionString );
    string command =
    "INSERT INTO Dealer...;SELEC T @@IDENTITY AS ID";
    cmd = new SqlCommand(comm and, cnn);
    try
    {
    cnn.Open(); //Point of Failure <===
    dr = cmd.ExecuteRead er();

    if (dr.Read())
    {
    dealerId = Convert.ToInt32 (dr[0]);
    dr.Close();

    if (dealer.BaseSys temInfo != null)
    {
    command = "INSERT
    INTO...";
    foreach (DealerBaseSyst em seg in
    dealer.BaseSyst emInfo)
    {
    cmd = new SqlCommand(comm and, cnn);
    cmd.ExecuteNonQ uery();
    }
    }
    }

    return dealerId;
    }
    catch (Exception e)
    {
    throw e;
    }
    finally
    {
    cnn.Close();
    //scope.Dispose() ;
    }
    //Commit transaction
    scope.Complete( );
    }
    }

    Hope someone can tell me why I get the error, and what I can do to
    make it work.

    Sincerely
    Svein Terje Gaup
Working...