Homepage | About Me | Mayday | Testing ASP.net Book | Follow me on Twitter | GitHub | SlideShare | RSS | DropBox referral link
Blog.BenHall.me.uk

The SqlParameter is already contained by another SqlParameterCollection.

Thursday, April 05, 2007

Yesterday, I came across a odd bug while debugging some code for a proof of 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 would then simply add the parameters to the SqlCommand:

SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
sqlCmd.Parameters.AddRange(queryParams);

 

After executing I would close the connection, then code would do some other stuff and a different query would 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 should 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 would post.  Carry on...

 

 

Technorati tags: ,

Blogger comments

At 8:41 PM, Anonymous Anonymous said...
Thanks, it saved me from tearing up my hears...    
At 8:39 PM, Anonymous Anonymous said...
Thanks for the post. I suffered the same problem, but your solution saved me tons o' time.

Thanks a ton.    
At 7:03 PM, Anonymous Anonymous said...
Really thanks for your tip!!!!    
At 2:31 PM, Anonymous Bruce Thompson said...
Thanks so much for the tip. Saved me a lot of work.    
At 9:50 PM, Anonymous Hans said...
Thanks for posting. Now that I know it's trivial but you definitely saved me a ton of frustration.    
At 4:44 AM, Anonymous Anonymous said...
Thanks for the post. I came across the same problem and this post was very useful and saved up my time.    
At 3:42 PM, Blogger Winter said...
You know, I would think this might work in the other direction: I have a sqlParameter that I want to use more than once--with its value intact--for a parent/child relation. I'm just inserting the value in two different query operations. Once I go to re-use the param in the child, however, I get this error. Odd.

Thanks much for this post: it's been helpful!    
At 6:57 PM, Anonymous Anonymous said...
Thank you. It saved lot of time.    
At 6:11 PM, Blogger Paul Fuller said...
Another "thanks" message for you--saved me time which is so valuable.    
At 2:04 PM, Blogger saxos said...
Wow, cool. Saved me a lot of time. So much abstractions, at last you forget about the things behind the whole system, like the Garbage Collector.    
At 12:49 PM, OpenID robspages said...
Found this via google for the error message. You saved me tons of time. Thanks.    
At 10:58 PM, Blogger johannes said...
Thank you. Cant imagine how much time this jus saved me!

Please do this as you run into future problems / oddities !

/JoLu    
At 7:05 AM, Blogger theo said...
Awesome, thx    
At 12:11 PM, Blogger Pramod Pawar said...
Well....this is not solving my probelm.. I have cmd parameters on framework level which is used as generice method and get called from outside world. Hence, I can't change using command parameters (cmd.parameters.clear())...

Is there any other way to play around this.... thanks