SQL error
stored procedure
database debugging
parameter missing
programming fix

Procedure expects parameter which was not supplied

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error "Procedure or function expects parameter which was not supplied" is one of the most common SQL Server errors developers encounter when calling stored procedures from application code. It means the stored procedure definition requires a parameter that your calling code did not provide. Understanding why this happens and how to fix it will save you significant debugging time, especially in larger codebases where stored procedures evolve independently from the application layer.

What the Error Looks Like

When SQL Server throws this error, you typically see a message like:

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

This tells you exactly which stored procedure failed and which parameter is missing. The fix seems obvious, but the root causes can be subtle.

Common Causes

Missing Parameter in Application Code

The most straightforward cause is simply forgetting to add a parameter when calling the stored procedure from C# or another language. Consider this stored 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

If your C# code omits the required @UserId parameter, you get the error:

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}

The fix is to 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 null Instead of DBNull.Value

This is the most deceptive cause. In C#, if a variable is null and you pass it directly as a parameter value, ADO.NET silently ignores the parameter entirely rather than sending a SQL NULL. SQL Server then complains that the parameter was 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);

The solution is to use DBNull.Value when the value is null:

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

This pattern ensures that a SQL NULL is sent to the server instead of the parameter being omitted.

Parameter Name Mismatch

A typo in the parameter name causes SQL Server to treat it as 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. However, @User_Id is a completely different parameter name and will cause the error.

Optional Parameters in SQL Server

SQL Server supports optional parameters with default values. If a parameter has a default, you do not need to supply it:

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. However, if you later remove the default value during a schema change and forget to update the application code, the error appears.

How to Debug

When you encounter this error, follow these steps:

First, check the stored procedure definition in SQL Server Management Studio to see all parameters and which ones have defaults:

sql
EXEC sp_helptext 'usp_GetUserById';

Second, log or inspect the parameters your application is actually sending. In C#, you can iterate over the parameter collection before executing:

csharp
1foreach (SqlParameter param in cmd.Parameters)
2{
3    Console.WriteLine($"{param.ParameterName} = {param.Value}");
4}

Third, test the stored procedure call directly in SSMS with the same parameter values to confirm it works outside your application.

Common Pitfalls

  • Passing C# null instead of DBNull.Value, which silently drops the parameter from the call
  • Misspelling a parameter name (e.g., @UserID vs @User_Id) and not realizing it
  • Updating a stored procedure to add a new required parameter without updating all calling code
  • Using AddWithValue with a value of null and assuming SQL Server receives NULL
  • Forgetting to set CommandType = CommandType.StoredProcedure, which causes the command text to be interpreted as raw SQL instead

Summary

  • The error occurs when a stored procedure's required parameter is not included in the call from application code
  • The most common hidden cause is passing C# null instead of DBNull.Value, which silently omits the parameter
  • Always use the (object)value ?? DBNull.Value pattern when a parameter might be null
  • Check for parameter name mismatches between your code and the stored procedure definition
  • Use sp_helptext or SSMS to verify which parameters are required and which have defaults
  • Log your parameter names and values before execution to catch issues early

Course illustration
Course illustration

All Rights Reserved.