Why does this dapper query return null?

asp.net-mvc-3 c# dapper mysql

Question

I have a mysql statement that I am passing to the dapper query method and it is returning a null object. I have created a POCO class to hold the returned result. I tested the query directly through a sql editor and it returned a result. Here is the code:

public ViewEmail GetViewEmail(string xId, int headerId)
{
    using (IDbConnection connection = new MySqlConnection(_connectionString))
    {
        string sql = "SELECT * FROM ViewEmail " +
                     "WHERE Header_id = @Header_id " +
                     "AND x_id= '@x_id'";

        var viewEmail = connection
            .Query<ViewEmail>(sql, new { Header_id = HeaderId, x_id = xId })
            .SingleOrDefault();

        return viewEmail;
    }
}

Accepted Answer

When using Prepared Statements, no need to wrap the parameter with '. Try this instead:

string sql = "SELECT * FROM ViewEmail " +
             "WHERE Header_id = @Header_id " +
             "AND x_id= @x_id";

See Documentation



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