Exception when AddWithValue parameter is NULL

Annoying, isn’t it.

You can use:

command.Parameters.AddWithValue("@application_ex_id",
       ((object)logSearch.LogID) ?? DBNull.Value);

Or alternatively, use a tool like “dapper”, which will do all that messing for you.

For example:

var data = conn.Query<SomeType>(commandText,
      new { application_ex_id = logSearch.LogID }).ToList();

I’m tempted to add a method to dapper to get the IDataReader… not really sure yet whether it is a good idea.

Leave a Comment