473,507 Members | 6,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error while using TransactionScope

RP
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 to add
sample codes of the two classes and later the code from a Form's
button click event which is giving problem.

========[ ModCon Code ]================================
public string ConString;
public SqlConnection myCN = new SqlConnection();

public void OpenConnection()
{
if (myCN.State == ConnectionState.Closed)
{
myCN.ConnectionString =
ReadConnectionStringFromFile();
}
try
{
if (myCN.State != ConnectionState.Open)
{
myCN.Open();
}
}
catch (Exception ex)
{

}
}

public void CloseConnection()
{
if (myCN.State == ConnectionState.Open)
try
{
myCN.Close();
}
catch (Exception ex)
{

}
}
================================================== =
=======[ ModRes Code ]=============================
public Int32 InsertNewRecord(string myQuery)
{
objModCon.OpenConnection();
SqlCommand cmdInsert = new SqlCommand(myQuery,
objModCon.myCN);
try
{
Int32 RecordsAffected = cmdInsert.ExecuteNonQuery();
return RecordsAffected;
}
catch (Exception ex)
{
MessageBox.Show("Routine: ModReUsable-
InsertNewRecord(" + myQuery + ") " + ex.ToString(), "Error:",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return 0;
}
finally
{
cmdInsert.Dispose();
objModCon.CloseConnection();
}
}
=================================================

=========[ Windows Form Code Sample ]=================
private void cmdProceed_Click(object sender, EventArgs e)
{
using (TransactionScope scope = new
TransactionScope())
{
//Start Generation
GenerateALL();
scope.Complete();
}
}
}

private void GenerateAll()
{
Loop Starts
Dim qInsert = "...............";
ModRes.InsertNewRecord(qInsert);

//Call Function A
FunctionA();

Dim qInsert = "...............";
ModRes.InsertNewRecord(qInsert);

FunctionB();
}

private void FunctionA()
{
Dim qUpdate = "..............."
ModRes.InsertNewRecord(qUpdate);
}

private void FunctionB()
{
................
}
================================================== =================

The above code from Windows Form Button Click event works well if I
don't use TransactionScope, whereas when I use TransactionScope it
displays error in Function A:

======[ ERROR ]======
ModRes.InsertNewRecord(........)
System.InvalidOperationException: ExecuteNonQuery requires an open and
available connection. The connection's current state is closed.
=====================

The exception is returned by Class ModRes, InsertNewRecord function.
Why? Does TransactionScope not allowing calls and execution of SQL in
other classes.

Oct 12 '07 #1
0 1422

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

Similar topics

3
4190
by: Jason Huang | last post by:
Hi, MyForm is a C# Windows Form. MyFom has 3 GroupBoxes. Now I wanna update data from the textboxes in those 3 GroupBoxes. I have function Update1 for GroupBox1 to update table Table1, Update2...
6
13948
by: cylt | last post by:
Hello, I'm using a TransactionScope with a ADO.NET SqlClient and i get the error : System.Runtime.InteropServices.COMException (0x8004D024): The transaction manager has disabled its support for...
2
2529
by: Spottswoode | last post by:
Hi I have developed a client/server application in C# that worked perfectly in my office but not so perfectly after deploying it to our client. A little information about the architecture first:...
3
3097
by: BLUE | last post by:
Only SQL Server 2005 or also DB2, Oracle and MySQL? Thanks, Luigi.
0
1542
by: =?Utf-8?B?Sm9l?= | last post by:
Very weird; I lost a day worth of work because of this problem. I have an ASP.NET application written in VB that is using MySQL database. Shortly, a page creates a Customer record in the database...
5
8623
by: RP | last post by:
I tried following code but it generates error at line (using .....): Error 23 The type or namespace name 'TransactionScope' could not be found (are you missing a using directive or an...
3
8003
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
3002
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...
2
2616
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
7220
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
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
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,...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.