I am using DapperLite, a port of Dapper, for use in Compact Framework. I get an ArgumentException, no other info in the below method at the specified line of the SqlMapper.cs file.
Really struggling with what to look for next or what's actually broken.
/// <summary>
/// Populate a references type from an IDataRecord by matching column names to property names.
/// </summary>
private static void PopulateClass(object objectClass, IDataRecord reader)
{
Type theType = objectClass.GetType();
PropertyInfo[] p = theType.GetProperties();
// Only set properties which match column names in the result.
foreach (string columnName in GetColumnNames(reader))
{
string colName = columnName;
object value = reader[colName];
PropertyInfo pi = p.FirstOrDefault(x => x.Name == colName);
if (pi == null || value == DBNull.Value) continue;
Type columnType = value.GetType();
Type actualType = Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType;
// Check for a directly assignable type
if (actualType == columnType || actualType == typeof(string) || actualType == typeof(int))
{
// Error generated here
pi.SetValue(objectClass, value, null);
}
else
{
value = actualType.GetMethod("Parse", new[] { typeof(string) }).Invoke(null, new[] { value });
pi.SetValue(objectClass, value, null);
}
}
}
The values at that point are objectClass = Models.Employee and value=1. The colName, from further up in the Method, is Id which is an int in the Employee model.
Added Employee Model based on Comment:
namespace BioPadPunchClock.Models
{
public class Employee
{
public int Id { get; set; }
public int CDB_Employee_PK { get; set; } //Employee PK in the Central DB. If .null., new emp record on this timeclock needs to be pushed to the CDB.
public string Name { get; set; }
//public string FaceImage { get; set; } //path to Jpeg image 136x90 pixels .\NAND\pictures\ named as <pk>.jpg
public DateTime? BirthDate { get; set; }
public string Status { get; set; } //Full Time – Hourly, Inactive, Terminated, Part Time - Hourly
public DateTime? LastPunch { get; set; }
public string LastPunchDirection { get; set; } //IN or OUT
public string PIN { get; set; } // 4 digit
public int CardNum { get; set; } //HID Card Number
public int CardFacCode { get; set; } //HID Card Facility Code
public string FpTemplate { get; set; } //Fingerprint Template (registered fingerprint)
public DateTime? LastUpdate { get; set; } //When this employee record was last sync’d to the CDB. This value will match exactly in the LDB and CDB after a successful sync.
public int DayTotal { get; set; } // Minutes cumulative for the current day at time of last punch
public int PayPeriodTotal { get; set; } //Minutes cumulative for the current pay period at time of last punch
public string Department { get; set; }
public string SSN { get; set; }
public bool ShouldPromptDept { get; set; } //Prompt employee to choose department each time
}
}
Further findings...
When I look at my Model.Employee at that point all of the string properties say Could not evaluate expression with a red exclamation point. Is this a clue or normal?
Further findings...
Not sure if this matters or not but when I step through the code and inspect the values I notice that the actualType == Int32 and the columnType==Int64 while the value it's evaluating is 1.
Not sure that makes any difference since there is a actualType.Equals(typeof(int)) but figured I'd mention it.
ArgumentExceptioncan be raised in two cases. Most probably, your case is the second. DoesEmployee.Idhave setter at all?ArgumentException was unhadledand under Details-->MessageArgumentException.actualType == typeof(string)will not work when you try SetValue using a string on that line. How do you plan to assign a string to an int? If it is the string "1" it will throw anArgumentExceptionHowever, at least on the desktop version, it will throw an informative error message telling you just that. However, it does to be an error in your logic. Also how can you assume that type int will work in the same condition? Also how can you assume the type has a Parse method if it doesn't meet those conditions?