C# - Failed to convert parameter value from a RuntimeMethodInfo to a String
Here is the code:
internal static void UpdateErrorLogTable(string type, string location, string method, Exception _error, string ftpDirectory, string remoteName, string localName)
{
try
{
using (SqlConnection databaseConnection = GetDatabaseConnection())
{
using (SqlCommand cmd = GetSqlCommand("ins_error_log", databaseConnection))
{
SqlParameter log_date = cmd.Parameters.Add("@log_date", SqlDbType.DateTime);
SqlParameter exception_type = cmd.Parameters.Add("@exception_type", SqlDbType.VarChar);
SqlParameter class_name = cmd.Parameters.Add("@class_name", SqlDbType.VarChar);
SqlParameter method_name = cmd.Parameters.Add("@method_name", SqlDbType.VarChar);
SqlParameter error_msg = cmd.Parameters.Add("@error_msg", SqlDbType.VarChar);
SqlParameter stack_trace_msg = cmd.Parameters.Add("@stack_trace_msg", SqlDbType.VarChar);
SqlParameter target_site_msg = cmd.Parameters.Add("@target_site_msg", SqlDbType.VarChar);
SqlParameter ftp_directory开发者_如何学C = cmd.Parameters.Add("@ftp_directory", SqlDbType.VarChar);
SqlParameter ftp_name = cmd.Parameters.Add("@ftp_name", SqlDbType.VarChar);
SqlParameter ftp_path = cmd.Parameters.Add("@ftp_path", SqlDbType.VarChar);
SqlParameter inner_exception_msg = cmd.Parameters.Add("@inner_exception_msg", SqlDbType.VarChar);
log_date.Value = m_todaysDate;
exception_type.Value = type;
class_name.Value = location;
method_name.Value = method;
error_msg.Value = _error.Message;
stack_trace_msg.Value = _error.StackTrace;
target_site_msg.Value = _error.TargetSite;
ftp_directory.Value = ftp_directory;
ftp_name.Value = remoteName;
ftp_path.Value = localName;
inner_exception_msg.Value = _error.InnerException;
cmd.ExecuteNonQuery();
}
} // using databaseConnection
} //end of TRY statement
catch (SqlException e)
{
logFile.SendToLog("SqlException", "Logs\\LogsDataAccess.cs", "UpdateErrorLogTable()", e);
} //end of SqlException CATCH statement
} //end of method UpdateErrorLogTable
The error I get is an InvalidCastException but it happens when attempting to execute
cmd.ExecuteNonQuery();
The part I can't figure out is why am I getting an InvalidCastException here and not at one of the points above where I'm assigning values. I stepped through the code in Debug line-by-line of this and it only gets the error when attempting to execute the code at that one line.
I performed a Google search for
Failed to convert parameter value from a RuntimeMethodInfo to a String
but no hits. So I searched for
convert from RuntimeMethodInfo to String
and still no luck.
To help you understand the code a little better, the statement
GetSqlCommand("ins_error_log", databaseConnection)
basically goes out and recognizes the info in quotes is a Stored Procedure and not a query string.
All this code above has been used throughout the application (written by someone else) and all I did was copy it and make the necessary modifications to the stored procedure name, the SQL parameters, and the values inserted.
I appreciate all your help!
*Note: I tried setting the TAG for this question as "RuntimeMethodInfo" but it wouldn't let me because I have less than a 1500 reputation. Maybe someone else can do it for me or give me the permissions to handle this.
It's this line:
target_site_msg.Value = _error.TargetSite;
Exception.TargetSite
is a MethodBase, not a string. Try changing to:
target_site_msg.Value = _error.TargetSite.Name;
Dunno if this is your problem, but it's not going to help:
inner_exception_msg.Value = _error.InnerException;
I think you want:
inner_exception_msg.Value = _error.InnerException.Message;
Try using the SqlCommand.Parameters.AddWithValue()
, along with the inner_exception_msg
param needing to be a string rather than an object. InnerException
is an Exception
object, but you'll need to supply a string.
Perhaps .Message
is good enough.
cmd.Parameters.AddWithValue("@log_date", m_todaysDate);
cmd.Parameters.AddWithValue("@exception_type", type);
cmd.Parameters.AddWithValue("@class_name", location);
cmd.Parameters.AddWithValue("@method_name", method);
cmd.Parameters.AddWithValue("@error_msg", _error.Message);
cmd.Parameters.AddWithValue("@stack_trace_msg", _error.StackTrace);
cmd.Parameters.AddWithValue("@target_site_msg", _error.TargetSite);
cmd.Parameters.AddWithValue("@ftp_directory", ftp_directory);
cmd.Parameters.AddWithValue("@ftp_name", remoteName);
cmd.Parameters.AddWithValue("@ftp_path", localName);
cmd.Parameters.AddWithValue("@inner_exception_msg", _error.InnerException.Message);
精彩评论