SOS!! Proc written in C# throwing errors !!

I have a stored proc which is being called by reporting services. It is written in C# and is a wrapper for executing other procs dynamically.It generates the following error time and again. Any help would be hugely appreciated as this is a production level issue.

Thanks

Shai

Error Message:

System.InvalidOperationException: The context connection is already in use. System.InvalidOperationException: at System.Data.SqlClient.SqlConnectionFactory.GetContextConnection(SqlConnectionString options, Object providerInfo, DbConnection owningConnection) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at StoredProcedures.USP_GenerateReport(String procname, String parameters) .
The Code

publicstaticvoid USP_GenerateReport(string procname,string parameters)

{

string[] parameterarray;

string parametername ="", parametervalue ="", separator =",";

//Create a connection in context of the calling app

SqlConnection objConn =newSqlConnection("Context connection= true");

objConn.Open();

try

{

//Create command object

SqlCommand objCmd =newSqlCommand(procname, objConn);

objCmd.CommandType =CommandType.StoredProcedure;

//Find the parameters for the target proc and add values

parameterarray = parameters.Split(separator.ToCharArray());

for (int i = 0; i <= parameterarray.Length - 1; i++)

{

parametername = parameterarrayIdea.Substring(0, parameterarrayIdea.IndexOf("="));

parametervalue = parameterarrayIdea.Substring(parameterarrayIdea.IndexOf("=") + 1, parameterarrayIdea.Length - parameterarrayIdea.IndexOf("=") - 1);

objCmd.Parameters.AddWithValue(parametername, parametervalue);

}

SqlContext.Pipe.ExecuteAndSend(objCmd);

}

catch(Exception e)

{

objConn.Close();

}

finally{

objConn.Close();

}

[4731 byte] By [shaikat] at [2008-2-4]
# 1

Are you calling this code from T-SQL as a stored-proc or from another CLR function?

You can have only one context connection open per stored-proc invocation, so that might be getting in the way.

pablo-ms at 2007-9-9 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 2
hey! i also ran into the same problem. In my case the function works fine as long as i let it complete its execution. Once it is interrupted in between, subsequent invocations give 'context connection in use' error.
sandeepmaurya at 2007-9-9 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...

SQL Server

Site Classified