Error thrown when executing a stored procedure that returns no results

c# dapper stored-procedures

Question

I am currently using dapper on one of our projects and we execute stored procedures all the time. In the first few methods everything was working fine when the stored procedure we execute returns rows.

Right now I'm facing an issue when I try to fetch data from an stored procedure that returns info when found. This is a very common use case (eg. logging users on the application). When calling the Query method, and the sproc does not return any row, dapper throws an ArgumentException with the message:

"When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn"

The code I'm using is:

using (var conn = new SqlConnection(connString))
{
    conn.Open();

    return conn.Query<Customer>(
            sql: "prc_GetCustomer",
            param: new { Parameter = p },
            commandType: CommandType.StoredProcedure).FirstOrDefault();
}

I'm aware that there is a Execute method that should be used when the procedure is expected to not return any rows but it's really not my situation. Also, dapper exception is misleading, since I'm not using multi-mapping.

Any ideas? Thanks!

Accepted Answer

If the query does not return any result grids, you should use Execute, not Query.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why