SQL error
stored procedure
database debugging
parameter missing
programming fix

Procedure or function expects parameter which was not supplied: how to fix

Codemia Knowledge Hub ·

The SQL Server error "Procedure or function expects parameter which was not supplied" means the stored procedure definition requires a parameter that your calling code did not provide. The usual causes are a forgotten parameter, a C# null passed instead of DBNull.Value, a misspelled parameter name, or a missing CommandType.StoredProcedure.

The message names both the procedure and the missing parameter:

 
Procedure or function 'usp_GetUserById' expects parameter '@UserId', which was not supplied.

The fix looks obvious, but the root cause can be subtle, especially when stored procedures evolve independently from the application code.

The parameter is missing from the call

Given this procedure:

sql
1CREATE PROCEDURE usp_GetUserById
2    @UserId INT,
3    @IncludeDeleted BIT = 0
4AS
5BEGIN
6    SELECT * FROM Users
7    WHERE Id = @UserId
8    AND (@IncludeDeleted = 1 OR IsDeleted = 0);
9END

C# code that omits the required @UserId fails:

csharp
1// This will throw the error — @UserId is missing
2using (var cmd = new SqlCommand("usp_GetUserById", connection))
3{
4    cmd.CommandType = CommandType.StoredProcedure;
5    // Forgot to add @UserId parameter!
6    cmd.Parameters.AddWithValue("@IncludeDeleted", 1);
7
8    var reader = cmd.ExecuteReader();
9}

Add the missing parameter:

csharp
1using (var cmd = new SqlCommand("usp_GetUserById", connection))
2{
3    cmd.CommandType = CommandType.StoredProcedure;
4    cmd.Parameters.AddWithValue("@UserId", 42);
5    cmd.Parameters.AddWithValue("@IncludeDeleted", 1);
6
7    var reader = cmd.ExecuteReader();
8}

Passing C# null instead of DBNull.Value

This is the most deceptive cause. If you pass a C# null as a parameter value, ADO.NET silently skips the parameter instead of sending SQL NULL, and SQL Server reports it as not supplied:

csharp
1string email = null; // Could come from user input
2
3// BUG: C# null causes the parameter to be skipped entirely
4cmd.Parameters.AddWithValue("@Email", email);

Use DBNull.Value when the value is null, so SQL NULL is actually sent:

csharp
cmd.Parameters.AddWithValue("@Email", (object)email ?? DBNull.Value);

Parameter name mismatch

A typo makes SQL Server see an unrecognized parameter while the required one appears missing:

csharp
// Stored procedure expects @UserId, but we send @UserID (different casing
// is fine) or @User_Id (different name entirely)
cmd.Parameters.AddWithValue("@User_Id", 42); // Wrong name!

SQL Server parameter names are case-insensitive, so @UserId and @USERID both work, but @User_Id is a different name and triggers the error.

Optional parameters and removed defaults

A parameter with a default value does not need to be supplied:

sql
1CREATE PROCEDURE usp_SearchUsers
2    @SearchTerm NVARCHAR(100),
3    @PageSize INT = 20,          -- Optional, defaults to 20
4    @PageNumber INT = 1           -- Optional, defaults to 1
5AS
6BEGIN
7    SELECT * FROM Users
8    WHERE Name LIKE '%' + @SearchTerm + '%'
9    ORDER BY Name
10    OFFSET (@PageNumber - 1) * @PageSize ROWS
11    FETCH NEXT @PageSize ROWS ONLY;
12END

Only @SearchTerm is required here. If a later schema change removes a default, or adds a new required parameter, and the application code is not updated, the error appears.

Forgetting CommandType.StoredProcedure

Without CommandType = CommandType.StoredProcedure, the command text is interpreted as raw SQL instead of a procedure call, so the parameters you added are not bound to the procedure.

Debugging steps

  1. Check the procedure definition in SQL Server Management Studio to see every parameter and which have defaults:
sql
EXEC sp_helptext 'usp_GetUserById';
  1. Log the parameters your application actually sends before executing:
csharp
1foreach (SqlParameter param in cmd.Parameters)
2{
3    Console.WriteLine($"{param.ParameterName} = {param.Value}");
4}
  1. Run the same call directly in SSMS with the same values to confirm it works outside your application.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design