Error message

IDbConnection Does Not Contain a Definition for ‘Query’ in C#: Fix

By Chandana on

When you start using Dapper with a stored procedure in Visual Studio, you may run into this compiler error:

Error 4 'System.Data.IDbConnection' does not contain a definition for 'Query'
and no extension method 'Query' accepting a first argument of type
'System.Data.IDbConnection' could be found
(are you missing a using directive or an assembly reference?)

The fix is usually one line. This post explains why the error appears and how to resolve it.

The Code That Triggers the Error

The error appears on a call like this:

var GenError = conn.Query<ClassName>(
    cmd,
    new { parameter1, parameter2 },
    commandType: CommandType.StoredProcedure
).ToList();

The code itself is valid Dapper syntax. The compiler just can’t find the Query method.

Why This Error Happens

IDbConnection is an interface from System.Data, and it has no method called Query. Query<T> comes from Dapper, which adds it to IDbConnection as an extension method.

Extension methods are only visible when the namespace that defines them is imported. If the compiler can’t see Dapper’s namespace, it reports that no Query definition or extension method exists. The error message hints at this with “are you missing a using directive or an assembly reference?”

The Solution

Step 1: Import the Dapper namespace

Add this to the top of your C# file:

using Dapper;

For most developers this is the whole fix. With the namespace imported, conn.Query<ClassName>(...) compiles.

Step 2: Make sure Dapper is installed

If using Dapper; itself shows an error such as “The type or namespace name ‘Dapper’ could not be found”, the package isn’t referenced in your project. Install it from NuGet:

Using the Package Manager Console:

Install-Package Dapper

Using the .NET CLI:

dotnet add package Dapper

Using the Visual Studio UI:

  1. Right-click your project in Solution Explorer.
  2. Choose Manage NuGet Packages.
  3. Search for Dapper and click Install.

After installing, add using Dapper; and rebuild.

The Corrected Code

using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;

public List<ClassName> GetData(string parameter1, string parameter2)
{
    using (IDbConnection conn = new SqlConnection(connectionString))
    {
        var result = conn.Query<ClassName>(
            "YourStoredProcedureName",
            new { parameter1, parameter2 },
            commandType: CommandType.StoredProcedure
        ).ToList();

        return result;
    }
}

How the Call Works

  • conn.Query<ClassName> runs the command and maps each row to a ClassName object.
  • cmd is the SQL text or, in your case, the stored procedure name.
  • new { parameter1, parameter2 } is an anonymous object whose property names become the parameter names. They must match the stored procedure’s parameters (@parameter1, @parameter2).
  • commandType: CommandType.StoredProcedure tells Dapper to treat cmd as a stored procedure, not an inline query.
  • .ToList() materializes the results into a list. It needs using System.Linq;.

Other Causes to Check

If the error persists after adding using Dapper;, check the following:

  • Wrong project. Dapper must be installed in the project that contains the code, not only in another project in the solution.
  • Target framework. Make sure your project’s target framework is supported by the Dapper version you installed.
  • Typos or casing. The namespace is Dapper and the method is Query, both case-sensitive.
  • Stale build. Clean and rebuild the solution (Build > Clean Solution, then Rebuild Solution), or restart Visual Studio if IntelliSense doesn’t refresh.
  • Missing System.Data reference. Older projects may need using System.Data; for IDbConnection and CommandType.

The Same Fix Applies to Other Dapper Methods

Dapper adds several extension methods to IDbConnection, so the same error can appear for any of them without using Dapper;:

  • Query and QueryAsync
  • QueryFirst, QueryFirstOrDefault, QuerySingle
  • Execute and ExecuteAsync
  • ExecuteScalar
  • QueryMultiple

Importing the namespace fixes all of them.