Dapper throwing invalid cast exception when using strongly-typed query parameters with Sybase ASE

c# dapper

Question

As the question title states, I'm getting Specified cast is not valid exceptions even though my stored proc returns an int and a DateTime and my class defines it the same.

public class Foo
{
    public int Id {get; set;}
    public DateTime CreatedDate {get; set;}
}

When I execute this query:

var results = connection.Query("spGetFoo", commandType: CommandType.StoredProcedure);

I get the following 2 exceptions:

InvalidCastException: Specified cast is not valid.
DataException: Error parsing column 1 (Id=1 - Decimal)

InvalidCastException: Specified cast is not valid.
DataException: Error parsing column 2 (CreatedDate=Mar  7 2012  5:52:08:276PM - String)

If I modify Foo's properties from int to decimal and DateTime to string it properly populates the values. But, this introduces an extra step of defining another class which holds the correct variable types and then mapping from Foo to the new class.

I also tried going through GetTypeDeserializer in the Dapper source to see what's going on but I'm not very familiar with IL emit.

Popular Answer

maybe the problem is in the Stored procedure, but in the meantime you can use

public class Foo
{
    private decimal _dbId;
    public decimal DbId 
    { 
        get { return _dbId; }
        set 
        { 
            _dbId = value;
            _id =(int)value;
        }
    }
    private String _dbCreadtedDate;
    public String DbCreadtedDate 
    { 
        get { return _dbCreadtedDate; }
        set 
        {
            _dbCreadtedDate = value;
            _createdDate = DateTime.Parse(_dbCreadtedDate);
        }
    }
    private int _id;
    public int Id 
    { 
        get { return _id; } 
        set { _id = value; _dbId = value; } 
    }
    private DateTime _createdDate;
    public DateTime CreatedDate 
    { 
        get { return _createdDate; } 
        set { _createdDate = value; _dbCreadtedDate = value.ToString(); } 
    }
}



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