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
public
static void USP_GenerateReport(string procname, string parameters){
string[] parameterarray; string parametername = "", parametervalue = "", separator = ","; //Create a connection in context of the calling app SqlConnection objConn = new SqlConnection("Context connection= true");objConn.Open();
try{
//Create command object SqlCommand objCmd = new SqlCommand(procname, objConn);objCmd.CommandType =
CommandType.StoredProcedure; //Find the parameters for the target proc and add valuesparameterarray = parameters.Split(separator.ToCharArray());
for (int i = 0; i <= parameterarray.Length - 1; i++)
{
parametername = parameterarray
.Substring(0, parameterarray
.IndexOf(
parametervalue = parameterarray
.Substring(parameterarray
.IndexOf(
objCmd.Parameters.AddWithValue(parametername, parametervalue);
}
SqlContext.Pipe.ExecuteAndSend(objCmd);
}
catch(Exception e){
objConn.Close();
}
finally{objConn.Close();
}

SOS!! Proc written in C# throwing errors !!
hatem elshenawy
Dave Rollins
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.