473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

When I try to open the connection, I get this error:
Test method
CDNDWUnitTestsNet.CDNDWFacadesNet_DealerAdminTest. DeleteDealerTest
threw exception: System.Transactions.TransactionException: The
operation is not valid for the state of the transaction..

Stack trace:
at CDNDWDataAccessNet.DealerAdminDataAccess.Save(Stri ng dealerXML)
in C:\DevProjects\CanalDigitalDealerWeb\Features\Ibs6 1\Source\DealerWeb
\Source\CDNDWDataAccessNet\DealerAdminDataAccess.c s:line 277
at CDNDWMiddlewareNet.DealerTransactions.Save(String dealerXml) in
C:\DevProjects\CanalDigitalDealerWeb\Features\Ibs6 1\Source\DealerWeb
\Source\CDNDWMiddlewareNet\DealerTransactions.cs:l ine 21
at CDNDWFacadesNet.DealerAdmin.SaveDealer(String dealerXML) in C:
\DevProjects\CanalDigitalDealerWeb\Features\Ibs61\ Source\DealerWeb
\Source\CDNDWFacadesNet\DealerAdmin.cs:line 29
at
CDNDWUnitTestsNet.CDNDWFacadesNet_DealerAdminTest. DeleteDealerTest()
in C:\DevProjects\CanalDigitalDealerWeb\Features\Ibs6 1\Source\DealerWeb
\CDNDWUnitTestsNet\CDNDWFacadesNet_DealerAdminTest .cs:line 184

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 (TransactionScope scope = new
TransactionScope(TransactionScopeOption.Required))
{

TransactionInterop.GetTransmitterPropagationToken( Transaction.Current);
this.SaveDealerCategory(dealer.Category); //this uses
a connection
this.SaveDealerChain(dealer.Chain); //this uses a
connection
cnn = new SqlConnection(connectionString);
string command =
"INSERT INTO Dealer...;SELECT @@IDENTITY AS ID";
cmd = new SqlCommand(command, cnn);
try
{
cnn.Open(); //Point of Failure <===
dr = cmd.ExecuteReader();

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

if (dealer.BaseSystemInfo != null)
{
command = "INSERT
INTO...";
foreach (DealerBaseSystem seg in
dealer.BaseSystemInfo)
{
cmd = new SqlCommand(command, cnn);
cmd.ExecuteNonQuery();
}
}
}

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
Jun 27 '08 #1
0 3162

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Zeya | last post by:
I have this code, which uses WMI to operate on Windows service from C# code. When Service.InvokeMethod is called, the method throws an exception: System.Management Operation is not valid due...
0
by: N! Xau | last post by:
Ciao, Hi, a datagrid on my form displays a table T, fine. On the form I have a button. I want to execute a query on T, just clicking that button. DB is Oracle. Button_Click code is: .......
6
by: Mukesh | last post by:
Hi, I am new to 2.0 framework. I am trying to use TransactionScope in the following code. I took four lables. Started TransactionScope scope. After setting values to three labels I throw an...
0
by: RP | last post by:
I have a two classes, first named "ModCon" has procedures written for connections and the second named "ModRes" contains functions and procedures that can be reused. For my question it is important...
3
by: Aleksey Timonin | last post by:
Hi guys, I tried to use TransactionScope on to defferent TableAdapters like this: using (TransactionScope transScope = new TransactionScope(TransactionScopeOption.Required)) {...
6
by: =?Utf-8?B?ZXhl?= | last post by:
Hi, I'm using TransactionScope to do a serie Insert, Update, Delete operations with the ExecuteNonQuery and some Selects with ExecuteScalar everything works fine with multiple connections in the...
3
by: Michael Schöller | last post by:
Hello, First of all english is not my natural language so please fogive me some bad mistakes in gramatic and use of some vocables :). I have a great problem here. Well I will not use it...
0
by: Svein Terje Gaup | last post by:
Hi, I have some code on .NET 2.0 that creates a TransactionScope and then tries to open a SQL Connection. When I try to open the connection, I get this error: Test method...
2
by: GaryDean | last post by:
When I use transactions with sql server I usually do this... using (TransactionScope scope = new TransactionScope) { using (SqlConnection1 = new SqlConnection . . . . . and this all works...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.