Isuru
Isuru

Reputation: 450

c# Handling exception for a wrong SQL query

I have seen similar questions regarding this topic which were asked earlier but couldn't find a solution for the above problem. I have an add-on programmed in c# for SAP B1. User has to enter some database column names to a UDT. Then my function run a query and fetch the data. But in case of user entering wrong column name to the table, I should have a proper error handling to catch this error and prevent the add-on from crashing. My code is given below.

try
{
 sqlString = "select " + databaseFieldName[increase] + " from OITM where ItemCode= '" + itemCode + "'";
 mRsitemCode.DoQuery(sqlString);
}
catch (System.Runtime.InteropServices.COMException ex)
{
 SBO_Application.MessageBox(ex.ToString(), 1, "Ok");
}
catch (Exception b)
{
  //string error;
  SBO_Application.MessageBox(b.ToString(), 1, "Ok");
}

But it doesn't run into any of my catch blocks. The error is given below.

An exception (first chance) of type 'System.Runtime.InteropServices.COMException' occurred in Item_Variation.dll.

Additional information: 1). [Microsoft] [SQL Server Native Client 10.0] [SQL Server] Invalid column name 'WhsCode'.

2). [Microsoft] [SQL Server Native Client 10.0] [SQL Server] Statement (s) Could not be prepared.

If a handler is available for this exception, the program may continue to run safely.

What am I doing wrong? Any help would be highly appreciated!

Upvotes: 0

Views: 1141

Answers (1)

Kinexus
Kinexus

Reputation: 12904

Your Visual Studio is configured to break on exceptions. If you use the Debug/Exceptions menu to select which errors you would like to break on, rather than let your code continue, you can do so.

Upvotes: 1

Related Questions