How to get detailed ExecuteNonQuery error message?

后端 未结 2 985
广开言路
广开言路 2021-01-19 18:51

I\'m working on my first ever assignment in ASP.NET. It\'s a website and I work in Visual Studio.

What I can\'t figure out is how to get a detailed error message to

相关标签:
2条回答
  • 2021-01-19 19:34

    Change your catch to be catch (Exception ex) and you can then use Response.Write(ex.Message)

    Ideally, you would have different catches for different exception types. This is a bucket approach to catching all exceptions.

    0 讨论(0)
  • 2021-01-19 19:44

    Change your catch to catch (SqlException ex) and catch (Exception ex)

            catch (SqlException ex)
            {
                StringBuilder errorMessages = new StringBuilder();
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #......" + i.ToString() + Environment.NewLine +
                                         "Message:....." + ex.Errors[i].Message + Environment.NewLine +
                                         "LineNumber:.." + ex.Errors[i].LineNumber + Environment.NewLine);
                }
                WriteToLog(FileLog, "SQL Error message: " + errorMessages.ToString() + Environment.NewLine);
                WriteToLog(FileLog, "Process has been finished with errors." + Environment.NewLine);
            }
            catch (Exception ex)
            {
                WriteToLog(FileLog, "Error message: " + ex.Message + Environment.NewLine);
                WriteToLog(FileLog, "Error stack trace: " + ex.StackTrace + Environment.NewLine);
                WriteToLog(FileLog, "Process has been finished with errors." + Environment.NewLine);
    
    0 讨论(0)
提交回复
热议问题