<MisbahAre...@discussions.microsoft.com> wrote:
> the if condition
> =A0 if(_mConnection.State =3D=3D ConnectionState.Open)
> needs to be changed
>
> try changing this to
> =A0 if(_mConnection.State !=3D 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 th=
e
> objects
>
> --
> Misbah Arefin
https://mcp.support.microsoft.com/profile/MISBAH.AREFINhttp:/=
/
www.linkedin.com/in/misbaharefin >
>
>
> "Pattnay...@gmail.com" wrote:
> > Hi,
>
> > Below is the piece of code i am using for my connection object &
> > database transaction.
>
> > =A0private SqlConnection _mConnection;
> > =A0 =A0 =A0 =A0 private SqlCommand _mCommand;
> > =A0 =A0 =A0 =A0 private SqlTransaction _mTransaction;
>
> > =A0 =A0 =A0 =A0 public SqlDataAdapter CreateDataAdaptor(string comText,
> > CommandType ComType, string Attribute)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 try
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand =3D new SqlCommand();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mConnection =3D new
> > SqlConnection(ConnectionString(Attribute));
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.Connection =3D _mConnection;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.CommandType =3D ComType;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.CommandText =3D comText;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (_mConnection.State !=3D ConnectionSt=
ate.Open)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mConnection.Open();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 SqlDataAdapter da =3D new SqlDataAdapter=
(_mCommand);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return da;
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 catch (Exception ex)
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 throw ex;
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 finally
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (_mConnection.State =3D=3D Connection=
State.Open)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mConnection.Close();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mConnection.Dispose();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.Dispose();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 public void Insert(string comText, Parameters Params,
> > CommandType ComType, string Attribute)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 try
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand =3D new SqlCommand();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mConnection =3D new
> > SqlConnection(ConnectionString(Attribute));
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.Connection =3D _mConnection;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.CommandType =3D ComType;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.CommandText =3D comText;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //_mCommand.Transaction =3D _mTransactio=
n;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (_mConnection.State !=3D ConnectionSt=
ate.Open)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mConnection.Open();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (ComType =3D=3D CommandType.StoredPro=
cedure)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (Params !=3D null)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 foreach (Parameter oPara=
m in Params)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
>
> > _mCommand.Parameters.AddWithValue(oParam.Name, oParam.Value);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.ExecuteNonQuer=
y();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.ExecuteNonQuery();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 catch (Exception ex)
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 throw ex;
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 finally
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (_mConnection.State =3D=3D Connection=
State.Open)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mConnection.Close();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mConnection.Dispose();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _mCommand.Dispose();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 }- Hide quoted text -
>
> - Show quoted text -