0

I am trying to call a piece of SQL with parameters in Dapper. It is NOT a stored procedure (I have that working fine with parameters)

inputCustomerName = "Our Customer";
inputStationName  = Null;

    var parameters = new
    {
        customerName = inputCustomerName ,
        stationName = inputStationName    
    };     

    ...

    using (var dbConn = dataProvider.CreateConnection)
    {
        dbConn.ConnectionString = connectionString;
        dbConn.Open();
        returnValue = dbConn.Query<T>(sql: sql, commandType: commandType, param: parameters);
        dbConn.Close();
    }

The relevant part of the SQL is

"    ...

     WHERE 
        Customer = ISNULL(@customerName,Customer)
        AND Station = ISNULL(@stationName,Station)
";

I keep getting "Invalid type owner for DynamicMethod". (I got this also when using DynamicParameters instead of the anomymous object).

The SQL runs fine on the database itself (given I declare and @populate @customerName and stationName).

I suspect I have done something quite simple wrong - can anyone enlighten me?

4
  • stackoverflow.com/questions/30435185/… is this helping? Commented May 4, 2017 at 12:35
  • Read it before I posted, Not my situaition, I don't think - I am passing in an object with properties as params, not an array. Commented May 4, 2017 at 12:37
  • Can you try to set the value of inputStationName to DBNull.Value instead of null? Commented May 4, 2017 at 12:43
  • I have previously tried populating both parameters with a value, it makes no difference. Commented May 4, 2017 at 12:48

1 Answer 1

0

The answer was, in the end, an issue in code not included in the question - for which I thoroughly apologise.

The issue was that T was an Interface, and that was what was causing Dapper to blow up. There's probably something under the hood that means you can only use classes, not interfaces as the type param to Query. Though I have never seen that restriction explicitly stated.

Shame, (but I can imagine a few reasons why)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.