The following solutions are available from overseas websites:
Yesterday, I came authentication ss a odd bug while debugging some code for a proof concept application.
The code itself was simple connecting to the database, executing two queries using different SqlCommand objects, but the same SqlParameter collection and returning the results to the user.
The method itself look two parameters, a SQL query as a string and a SQL Parameter []. I wowould then simply add the parameters to the SqlCommand:
SqlCommand sqlCmd = new SqlCommand (SQL, sqlConn );
SqlCmd. Parameters. AddRange (queryParams );
After executing I wocould close the connection, then code wocould do some other stuff and a different query wocould be executed but using the same parameter collection. in theory, there is no reason why this wouldn't work however the code was throwing an exception with the error "The SqlParameter is already contained by another SqlParameterCollection. "How odd, I had closed the connection and it shouldn't be referred so I shocould be able to add it.
Turns out, or at least I think, that the garage collection isn' t kicking in quick enough and so the reference is still alive. quick fix was to clear the parameters on the SqlCommand object after execution.
SqlCmd. Parameters. Clear ();
Tripped me up for a little bit so I thought I wocould post. Carry on...