The SqlParameter is already contained by another SqlParameterCollection - Does using cheat?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The error The SqlParameter is already contained by another SqlParameterCollection occurs in ADO.NET when you try to add a SqlParameter object to a SqlCommand.Parameters collection while it is still attached to a different command's parameters. This commonly happens when reusing parameter objects across multiple commands or when a previous command was not properly disposed.
Why This Error Occurs
Each SqlParameter object can only belong to one SqlParameterCollection at a time. When you create a parameter and add it to a command, the parameter maintains a reference to that collection. If you then try to add the same parameter instance to another command, ADO.NET throws this error:
Does the using Statement Help?
The using statement disposes the SqlCommand, which calls Dispose() on the command object. However, disposing a SqlCommand does not automatically clear its Parameters collection or detach the parameters. This is a common misconception:
The behavior depends on the .NET version and provider implementation, but you should never rely on disposal to detach parameters.
How to Fix It
Option 1: Clear Parameters Before Reuse
Explicitly clear the parameters collection before adding to a new command:
Option 2: Create New Parameters for Each Command (Recommended)
The cleanest approach is to create new parameter instances for each command:
Option 3: Use a Helper Method
For complex scenarios where the same parameters appear in multiple queries, create a factory:
Option 4: Clone the Parameter
If you need to reuse a parameter template:
Common Scenario: Loops
This error frequently appears in loops where commands are created but parameters are reused:
Common Pitfalls
- Assuming
usingcleans up parameters: Theusingstatement callsDispose()on the command, but this does not reliably detach parameters from the collection. Always callParameters.Clear()explicitly if reusing parameters. - AddWithValue type inference:
AddWithValueinfers the SQL type from the .NET type, which can cause performance issues (e.g.,stringmaps tonvarchar(max)instead ofnvarchar(50)). For performance-critical code, useParameters.Addwith explicit types. - Thread safety:
SqlParameterobjects are not thread-safe. Never share parameter instances across threads or concurrent commands. - Dapper and other ORMs: If you use Dapper or Entity Framework, they handle parameter creation internally, so this error does not apply. It is specific to raw ADO.NET usage.
Summary
- Each
SqlParametercan only belong to oneSqlParameterCollectionat a time - The
usingstatement does not automatically detach parameters from a disposed command - Best practice: create new parameters for each command rather than reusing instances
- If reusing is necessary, call
cmd.Parameters.Clear()before adding parameters to a new command - In loops, always create fresh parameter instances per iteration
Related reading
- The type or namespace name 'Entity' does not exist in the namespace 'System.Data
- There can be only one auto column
- Thinking of storing serialized java objects into cassandra as JSON. What is the catch?
- Thread safe Entity Framework 6
- The State of Linkers for .NET apps aka Please Sir, May I have a Linker 2009 edition
- The type is defined in an assembly that is not referenced, how to find the cause?
- Throw an error preventing a table update in a MySQL trigger
- TiDB CREATE FUNCTION returns error

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.