Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find which sql server parameter is throwing an exception in .net?

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();
         }
     }
}
like image 731
StatsViaCsh Avatar asked Feb 02 '26 15:02

StatsViaCsh


1 Answers

I do not see 70 parameters in your code but assuming you have 70, here are some pointers to debug.

1) Since it is a decimal conversion error, and assuming the most common case of precision not enough to support your value, you can write a quick script and run it in sql server to see which one is at fault.
Untested script below:

StringBuilder sb = new StringBuilder();
            foreach (SqlParameter param in sqlParams)
            {
                sb.Append("declare " + param.ParameterName + " as " + param.SqlDbType + Environment.NewLine);
                sb.Append("set " + param.ParameterName + " = " + param.Value + Environment.NewLine);
            }
            string sqlTestString = sb.ToString();

2) You can use SQL profiler to see the parameters that have been passed to the SQL server during execution. You can also use other debugging information provided by the SQL profiler.

3) Not all parameter are going to be decimal. Assuming you have a classic case of 20 - 30% decimal parameters, that would amount to 14 - 20 parameters. Sometimes, it is worth to manually inspect them.

like image 167
Antony Thomas Avatar answered Feb 04 '26 03:02

Antony Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!