How to get a detailed ExecuteNonQuery error message?

I am working on my first job in ASP.NET. This is a website and I work in Visual Studio.

I cannot figure out how to get a detailed error message that will be displayed when ExecuteNonQuery fails.

I am using an OleDb connection, so I assume that I need to use an 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?
}
+3
source share
1 answer

Change catchto catch (Exception ex)and then you can useResponse.Write(ex.Message)

. .

+6

All Articles