How to get detailed ExecuteNonQuery error message?

浪子不回头ぞ 提交于 2020-01-11 09:21:07

问题


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 be displayed when ExecuteNonQuery fails.

I'm using OleDb connection, so my guess is I have to use OleDbException or OleDbError to get a detailed error message.

Basically, the question is - how do I update this code to have a detailed error message IF ExecuteNonQuery fails?

string v1 = Request["v1"];                                    
string v2 = Request["v2"];
sql2 = "INSERT INTO table(one, two) VALUES('" + v1 + "', '" + v2  + "')";

System.Data.OleDb.OleDbConnection con2 = new System.Data.OleDb.OleDbConnection();
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data          Source=C:/Users/BB/Desktop/Database.mdb";
con2.Open();
OleDbCommand command = new OleDbCommand(sql2, con2);

try
{
    command.ExecuteNonQuery();
}
catch
{
    Response.Write("Error!");
    // detailed error message here?
}

回答1:


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.



来源:https://stackoverflow.com/questions/6084760/how-to-get-detailed-executenonquery-error-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!