0
votes

I'm trying to log an error message to a database when an exception is thrown. The idea is to have the exception be logged into the database so it can be investigated at a later time.

This is the code:

private void btnGroundMVC_Click(object sender, RoutedEventArgs e)
    {
    try
            {
                Process.Start(Library.paiplib.GroundMVC);
            }
            catch (Exception ex)
            {
                SqlConnection sqlconn = new SqlConnection(connectionString);
                String timeStamp = Library.staticlib.getTimestamp(DateTime.Now);
                SqlCommand cmd = new SqlCommand(errorlog, sqlconn);

                cmd.Parameters.AddWithValue("@timeStamp", timeStamp);
                cmd.Parameters.AddWithValue("@ex", ex);

                sqlconn.Open();
                cmd.ExecuteNonQuery();
                sqlconn.Close();

                MessageBox.Show(Library.paiplib.error + ex,"Error Loading PAIP", MessageBoxButton.OK,MessageBoxImage.Error);

            }

This is the error message while debugging:

"An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: No mapping exists from object type System.ArgumentException to a known managed provider native type."

This says that is at the cmd.ExecuteNonQuery();

2

2 Answers

2
votes

The error is pretty clear: the SQL Server provider doesn't know of an Exception-compatible column type.

If you'll simply want to store the string representation of the exception:

cmd.Parameters.AddWithValue("@ex", ex.ToString());
-1
votes

i would use

cmd.Parameters.Add("@ex", SqlDbType.NVarChar).Value