Link to home
Start Free TrialLog in
Avatar of techlinden
techlindenFlag for United States of America

asked on

SQLserver database connection issue: Invalid operation. The connection is closed.

Hi Tech Team,

i am checking connection state and opening if connection is not opened but getting error at  cmd.ExecuteReader(CommandBehavior.CloseConnection);

here is my code:

if (cmd.Connection.State == System.Data.ConnectionState.Open)
                {

                }
                else
                {
                    cmd.Connection.Open();
                }

                DR = cmd.ExecuteReader(CommandBehavior.CloseConnection);


error details:
Message: Invalid operation. The connection is closed. Stacktrace:    at System.Data.ProviderBase.DbConnectionClosed.CreateReferenceCollection()
   at System.Data.ProviderBase.DbConnectionInternal.AddWeakReference(Object value, Int32 tag)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)

Thanks in advance
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Is this really your code?

Why the
                {

                }
that does nothing.

In my experience, when somebody removes code to make a demonstration simpler, they usually remove the code that was causing the error.
Avatar of Ryan Chong
the codes look ok for me, I tested similar code locally.

do you referring to object cmd in other place between the codes you posted? is that possible you closed the connection before the execution of line:  
DR = cmd.ExecuteReader(CommandBehavior.CloseConnection);?

if (cmd.Connection.State == System.Data.ConnectionState.Open)
                {

                }
                else
                {
                    cmd.Connection.Open();
                }

//You closed the connection here??

                DR = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ASKER CERTIFIED SOLUTION
Avatar of Ammar Gaffar
Ammar Gaffar
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
techlinden, do you still need help with this question?