Joe M
Joe M

Reputation: 3440

Can Dapper handle null values in a return?

I'm calling the Dapper extended function Query for the IDbConnection class. The problem is, some of the values its returning are NULL in the database. It keeps complaining about them, so I've been converting the members of my return class to nullable types. But I'm thinking I may end up doing this for almost every member, because the data is often incomplete. This is being changed from older code that uses the DataRow class and is able to assign empty values from it to string members of my return class without any problems using just ToString.

public class ProductInfo
{
    public Char Department { get; set; }
}

This works:

ProductInfo myProduct = new ProductInfo{};
myProduct.Department = dataRow["department"].ToString();

dataRow["department"] shows as {} and myProduct.Department shows as "".

This gives an exception:

List<ProductInfo> myProduct = new List<ProductInfo>();
myProduct = dbConnection.Query<ProductInfo>(command, parameters).ToList();

Wtih the error: Error parsing column 1 (department=<null>) Details: System.ArgumentNullException: Value cannot be null

Am I missing a step in the process Dapper uses to assign values? Or is extensive use of nullable types standard practice with it?

Upvotes: 0

Views: 3228

Answers (1)

user47589
user47589

Reputation:

This isn't a limitation with Dapper; it's how your model is setup.

A char type isn't nullable.

Change your model to char? to make it nullable.

Upvotes: 2

Related Questions