Hello,
The exception stack trace is not very informative:
System.InvalidCastException: Specified cast is not valid.
à System.Data.DataTableReader.GetInt32(Int32 ordinal)
à Polyright.xxx.yyy.DatabaseAccess.GetAccountByPersonId(Int32 personId) dans ...\DatabaseAccess.cs:ligne ...
This occurs when I do:
Code: Select all
public Account GetAccountByPersonId(int personId) {
try {
DbCommand dbCommand = database.GetStoredProcCommand("xxx.GetAccountByPersonId");
database.AddParameter(dbCommand, "personId", DbType.Int32, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, personId);
dbCommand.Parameters.Add(CreateCursorParameter("cursor"));
IDataReader reader = database.ExecuteDataSet(dbCommand).CreateDataReader();
Account account = null;
int idx;
if (reader.Read()) {
account = new Account();
// CORRECT : Use reader.GetDecimal()
idx = reader.GetOrdinal("accountId");
if (!reader.IsDBNull(idx)) account.AccountId = Convert.ToInt32(reader.GetDecimal(idx));
// ERROR : InvalidCastException
idx = reader.GetOrdinal("accountstatusid");
if (!reader.IsDBNull(idx)) account.AccountStatusId = reader.GetInt32(idx);
...
}
return account;
} catch (Exception e) {
log.Error("GetAccountByPersonId : un problème est survenu", e);
throw;
}
}
Although I use Enterprise Library 2.0, the underlying database provider is OraDirect .NET 3.55.20.0. I don't know where is the responsability of doing the data types mapping.
In the code sample above, the two fields are NUMBER columns in Oracle and I thought it makes sense to call GetInt32, but for all Oracle number columns, even for a double, I have to call GetDecimal() and then use Convert.ToInt32 or Convert.ToDouble.
Sorry, I have not the time to build a sample project, and it is not a blocking problem.
Thanks