Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Error while using TransactionScope

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RP - 12 Oct 2007 13:51 GMT
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.
Peter Bromberg [C# MVP] - 12 Oct 2007 16:11 GMT
I think if you read the documentation on the TransactionScope class

http://msdn2.microsoft.com/en-us/library/system.transactions.transactionscope.aspx

-- it will become much clearer how it must be used. It requires a valid and
open connection to be able
to handle transactions across multiple method calls. By definition, this
means the same connection object must be visible to and in the scope of each
call.
In your case the exception message is quite clear - you don't even have an
open connectioni.

-- Peter
Recursion: see Recursion
site:  http://www.eggheadcafe.com
unBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder:    http://www.blogmetafinder.com

> I have a two classes, first named "ModCon" has procedures written for
> connections and the second named "ModRes" contains functions and
[quoted text clipped - 118 lines]
> Why? Does TransactionScope not allowing calls and execution of SQL in
> other classes.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.