3

I'm trying to do the following:

var p = new DynamicParameters();
p.Add(...);
p.Add(...);
// ideally I want to insert null for end date, but I also tried DateTime.MinValue
// or some other date, but doesn't help
p.Add("@endDate", null, DbType.DateTime, ParameterDirection.Input);
using (var con = new SqlConnection(ConnectionString))
{
    // stored procedure does 'select SCOPE_IDENTITY()' to return the id of inserted row.
    int id = con.Query<int>("StoredProcedureName", p, commandType: CommandType.StoredProcedure).First();
}

This is throwing "InvalidCastException" and I believe this is becase of the date. I previously had datetime2(20) for the endDate in the sql db, changed it to datetime to see if it fixes it, but doesn't. Can anyone help?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Shankar
  • 1,634
  • 1
  • 18
  • 23

1 Answers1

2

The invalid cast here is that SCOPE_IDENTITY() actually returns decimal. The parameters are working fine.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • thank you for the quick response. i did search for dapper related issues and found some discussion about SCOPE_IDENTITY(), but I didn't pay enough attention to realize that it is related to this issue. – Shankar Feb 18 '13 at 03:47