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 / .NET Framework / ADO.NET / March 2008

Tip: Looking for answers? Try searching our database.

Connection Open Error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pattnayaks@gmail.com - 26 Mar 2008 04:10 GMT
Hi,

Below is the piece of code i am using for my connection object &
database transaction.

private SqlConnection _mConnection;
       private SqlCommand _mCommand;
       private SqlTransaction _mTransaction;

       public SqlDataAdapter CreateDataAdaptor(string comText,
CommandType ComType, string Attribute)
       {
           try
           {
               _mCommand = new SqlCommand();
               _mConnection = new
SqlConnection(ConnectionString(Attribute));
               _mCommand.Connection = _mConnection;
               _mCommand.CommandType = ComType;
               _mCommand.CommandText = comText;
               if (_mConnection.State != ConnectionState.Open)
               {
                   _mConnection.Open();
               }
               SqlDataAdapter da = new SqlDataAdapter(_mCommand);
               return da;
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               if (_mConnection.State == ConnectionState.Open)
               {
                   _mConnection.Close();
                   _mConnection.Dispose();
                   _mCommand.Dispose();
               }
           }
       }
       public void Insert(string comText, Parameters Params,
CommandType ComType, string Attribute)
       {
           try
           {
               _mCommand = new SqlCommand();
               _mConnection = new
SqlConnection(ConnectionString(Attribute));
               _mCommand.Connection = _mConnection;
               _mCommand.CommandType = ComType;
               _mCommand.CommandText = comText;
               //_mCommand.Transaction = _mTransaction;
               if (_mConnection.State != ConnectionState.Open)
               {
                   _mConnection.Open();
               }
               if (ComType == CommandType.StoredProcedure)
               {
                   if (Params != null)
                   {
                       foreach (Parameter oParam in Params)
                       {

_mCommand.Parameters.AddWithValue(oParam.Name, oParam.Value);
                       }
                       _mCommand.ExecuteNonQuery();
                   }
               }
               else
               {
                   _mCommand.ExecuteNonQuery();
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               if (_mConnection.State == ConnectionState.Open)
               {
                   _mConnection.Close();
                   _mConnection.Dispose();
                   _mCommand.Dispose();
               }
           }
       }
Misbah Arefin - 26 Mar 2008 16:23 GMT
the if condition
 if(_mConnection.State == ConnectionState.Open)
needs to be changed

try changing this to
 if(_mConnection.State != ConnectionState.Closed)

since the connection could be busy executing some query or fetching
records... its still open but the status code is different

on a side note if all you need to do is call dispose on your objects then
use the using construct which quarantees that dispose will be called on the
objects

Signature

Misbah Arefin
https://mcp.support.microsoft.com/profile/MISBAH.AREFIN
http://www.linkedin.com/in/misbaharefin

> Hi,
>
[quoted text clipped - 84 lines]
>             }
>         }
Pattnayaks@gmail.com - 28 Mar 2008 06:53 GMT
On Mar 27, 2:23 am, Misbah Arefin
<MisbahAre...@discussions.microsoft.com> wrote:
> the if condition
>   if(_mConnection.State == ConnectionState.Open)
[quoted text clipped - 104 lines]
>
> - Show quoted text -

Thanks..it worked and i removed _mConnection.Dispose() from finally
block.

Romi

Rate this thread:







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.