SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
This error means you are adding the wrong kind of object to SqlCommand.Parameters. The collection accepts SqlParameter objects, but your code is passing a plain string instead. In practice, this usually happens because the wrong Add overload was called or because someone tried to add parameter values without creating actual parameter objects.
What the Collection Expects
SqlParameterCollection belongs to ADO.NET and stores parameter definitions for a SQL command.
A correct example looks like this:
Here the collection receives a real SqlParameter object, which is what it expects.
A Common Wrong Pattern
The error often appears when code does something like this:
That adds a string object, not a SqlParameter instance, so the collection rejects it.
The fix is to use the overload that creates or supplies a parameter correctly.
Correct Ways to Add Parameters
One clear approach is to create the parameter explicitly.
Another common option is AddWithValue.
And another is to create the parameter with type information.
This third form is often better when you want explicit control over SQL types.
Why the Wrong Overload Happens
ADO.NET has several Add overloads, which can make the API easy to misuse if you rely on guesswork.
Developers often assume that passing a string is enough because they are thinking of the parameter name, but the collection needs more than the name. It needs either:
- a real
SqlParameterobject - or an overload that knows how to construct one properly
That is why reading the overload signature matters.
Full Runnable Example
This avoids the error because every item added to the collection is a proper parameter.
Why Explicit Types Can Be Better
AddWithValue is convenient, but it can infer a SQL type you did not intend. That is why many teams prefer the explicit typed overloads for production database code.
So the real best practice is often:
- use typed
Addoverloads when type precision matters - use
AddWithValueonly when the inferred type is obviously correct
Common Pitfalls
The biggest mistake is calling Parameters.Add("@Name") and assuming the collection will infer the rest.
Another mistake is using the parameter name string where the API expects a SqlParameter object.
A third issue is fixing the error with AddWithValue everywhere without considering whether the inferred SQL type is appropriate.
Summary
- The error means a string was added where
SqlParameterCollectionexpected aSqlParameter - Use
new SqlParameter(...), typedAdd(...), orAddWithValue(...)correctly - The problem usually comes from calling the wrong
Addoverload - Explicit parameter types are often better than relying on inference
- Parameterized commands should be built from real parameter objects, not plain strings
Related reading
- start index at 1 for Pandas DataFrame
- Static Indexers?
- steps for making a non-distributed db to distributed db [talking about lmdb specifically]
- storage engine how to quickly find that key is not exist
- SslStream asynchronous methods always return an IAsyncResult where CompletedSynchronously is True. Why?
- Stack capacity in C
- squad2.0 training error THCudaCheck FAIL file/pytorch/aten/src/THC/THCGeneral.cpp line50 error100 no CUDA-capable device is detected
- SSE connection not established

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.