techlinden
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(CommandB ehavior.Cl oseConnect ion);
here is my code:
if (cmd.Connection.State == System.Data.ConnectionStat e.Open)
{
}
else
{
cmd.Connection.Open();
}
DR = cmd.ExecuteReader(CommandB ehavior.Cl oseConnect ion);
error details:
Message: Invalid operation. The connection is closed. Stacktrace: at System.Data.ProviderBase.D bConnectio nClosed.Cr eateRefere nceCollect ion()
at System.Data.ProviderBase.D bConnectio nInternal. AddWeakRef erence(Obj ect value, Int32 tag)
at System.Data.SqlClient.SqlC ommand.Fin ishExecute Reader(Sql DataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlC ommand.Run ExecuteRea derTds(Com mandBehavi or cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlC ommand.Run ExecuteRea der(Comman dBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlC ommand.Run ExecuteRea der(Comman dBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlC ommand.Exe cuteReader (CommandBe havior behavior, String method)
at System.Data.SqlClient.SqlC ommand.Exe cuteReader (CommandBe havior behavior)
Thanks in advance
i am checking connection state and opening if connection is not opened but getting error at cmd.ExecuteReader(CommandB
here is my code:
if (cmd.Connection.State == System.Data.ConnectionStat
{
}
else
{
cmd.Connection.Open();
}
DR = cmd.ExecuteReader(CommandB
error details:
Message: Invalid operation. The connection is closed. Stacktrace: at System.Data.ProviderBase.D
at System.Data.ProviderBase.D
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
Thanks in advance
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(CommandB ehavior.Cl oseConnect ion);?
if (cmd.Connection.State == System.Data.ConnectionStat e.Open)
{
}
else
{
cmd.Connection.Open();
}
//You closed the connection here??
DR = cmd.ExecuteReader(CommandB ehavior.Cl oseConnect ion);
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(CommandB
if (cmd.Connection.State == System.Data.ConnectionStat
{
}
else
{
cmd.Connection.Open();
}
//You closed the connection here??
DR = cmd.ExecuteReader(CommandB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
techlinden, do you still need help with this question?
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.