I'm trying to execute a SQL Server stored procedure from .net. I'm getting the error
Error converting data type numeric to decimal
but I'm having a hard time determining which SQL parameter is the culprit.
There are 70, so I'm sure there's an easier way than a manual search.
Is it somewhere in the exception box, in "view detail?" Thanks in advance..
Relevant code (hopefully this is helpful... I didn't include all parameters, but this is the pattern):
public void WriteKeyStatToDB(KeyStatisticsDataCollection.KeyStatsDP dp)
{
SqlParameter symbol = SqlParameterFactory("@symbol", SqlParamInOrOut.Input, SqlDbType.Char);
SqlParameter dateAdded = SqlParameterFactory("@dateAdded", SqlParamInOrOut.Input, SqlDbType.Date);
SqlParameter tradeDate = SqlParameterFactory("@tradeDate", SqlParamInOrOut.Input, SqlDbType.Date);
SqlParameter marketCapIntra = SqlParameterFactory("@marketCapIntra", SqlParamInOrOut.Input, SqlDbType.Money);
SqlParameter entVal = SqlParameterFactory("@entVal", SqlParamInOrOut.Input, SqlDbType.Money);
SqlParameter trailingPE = SqlParameterFactory("@trailingPE", SqlParamInOrOut.Input, SqlDbType.Decimal);
SqlParameter forwardPE = SqlParameterFactory("@forwardPE", SqlParamInOrOut.Input, SqlDbType.Decimal);
symbol.Value = dp.Symbol;
dateAdded.Value = dp.TradeDate;
tradeDate.Value = dp.TradeDate;
marketCapIntra.Value = dp.MarketCapIntraDay;
entVal.Value = dp.EntValue;
trailingPE.Value = dp.TrailingPE;
forwardPE.Value = dp.ForwardPE;
List<SqlParameter> sqlParams = new List<SqlParameter> {symbol, dateAdded, tradeDate, marketCapIntra, entVal, trailingPE, forwardPE};
ExecuteSproc("spAddKeyStatDP", sqlParams, SqlConnection);
}
private static SqlParameter SqlParameterFactory (String ParamName, SqlParamInOrOut Direction,
SqlDbType SqlType)
{
SqlParameter param = new SqlParameter
{
ParameterName = ParamName,
SqlDbType = SqlType
};
if (Direction == SqlParamInOrOut.Input)
param.Direction = ParameterDirection.Input;
else
param.Direction = ParameterDirection.Output;
return param;
}
private static void ExecuteSproc(String CommandName, List<SqlParameter> ParamList, SqlConnection SqlConn, int Timeout = 0)
{
using (SqlCommand cmd = new SqlCommand(CommandName, SqlConn))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter sp in ParamList)
{
if (sp.Value == null)
sp.Value = DBNull.Value;
cmd.Parameters.Add(sp);
}
cmd.CommandTimeout = Timeout;
cmd.ExecuteNonQuery();
}
}
}