سؤال

We have error handling that logs exceptions into our database. An error is logging that is just a SQL Server column name, nothing more. The errors we log are the exception message concatenated with the inner exception. So we'd expect to see the kind of exception and more detail, but instead all we see is ProblemField

We are writing in C# on the .Net 2.0 Framework and using SQL Server 2005 for the DB.

Here's the code, along with issues I noticed.

using (SqlCommand cmd = new SqlCommand("MyStoredProcedure", connection))
{
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.AddWithValue("@PARAM1", var1);
   cmd.Parameters.AddWithValue("@PARAM2", var2);
   cmd.Parameters.AddWithValue("@PARAM3", var3);

   using (SqlDataReader rdr = cmd.ExecuteReader())
   {
       if (rdr.HasRows)
       {
           rdr.Read();

           var4 = (double)rdr["ProblemField"];
           var5 = (double)rdr["OtherField"];
       }
       else
       {
           var4 = var5 = 0.0;
       }
   }
}

I changed these reads of rdr["ProblemField"] and rdr["OtherField"] to use a Double.TryParse instead.

Select of MyStoredProcedure:

SELECT TOP 1 [anbrID]
        ,[col1]
        ,[col2]
        ,[col3]
        ,COALESCE([ProblemField], 0.0)
        ,COALESCE([OtherField], 0.0)
        ,[col6]
        FROM MyTable
        WHERE (col1=@PARAM1 OR
            CAST(@PARAM2 AS int) between CAST(col2 AS int) and CAST(col3 AS int)) AND
            col6 =@PARAM3
        ORDER BY id DESC

I have changed these COALESCEs to also have an as "ProblemField," etc..

Bottom line: Is this kind of exception message normal? Do my fixes seem reasonable for such a vague error?

هل كانت مفيدة؟

المحلول

This is why (among other reasons) you should log the whole exception (using .ToString) so that you get the stacktrace and the exception type.

I'll hazard a guess though, that it's an ArgumentOutOfRangeException thrown by the SqlDataReader - I believe that only gives the field parameter without any other message. You currently have no 'as' for the coalesced columns which would definitely cause this. You say you added it, but I suspect it was incorrect somehow.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top