Compartir a través de


eConnect exception handling

The eConnectException classes produces eConnect-specific error information. You typically use the eConnectExceptions in the following situations.

  • You add code to your .NET project that detects eConnect-specific errors. You then add code that specifies the actions to take when an eConnect error occurs.
  • You use the eConnectException class to create and throw a new exception object. For example, you use the errMessage parameter of the eConnectException class to add error information that specifies where the error occurred in your .NET application.

If you use the classes in Microsoft.Dynamics.GP.eConnect, you should include code to catch and handle eConnect exceptions from the methods of those classes. The most common exception handling technique is the Try/Catch block. For example, you place a Try block around a call to the CreateEntity method. You then use a Catch block to handle the eConnectException type. Typically, you add code to the Catch block that attempts to correct the error, reports the error to the user, or records error information to a log.

For more information about the eConnect exceptions, see eConnectException Class.

The following Visual Basic example shows how to use a Try/Catch block to handle an eConnectException. Notice how the first Catch statement handles eConnectExceptions while the second Catch handles all other exception types. In this example, the application displays the error information from the message property of the exception in a textbox control.

Dim ConnectionString As String
Dim eConnectResult As Boolean
Dim eConnectObject As New eConnectMethods
Dim xmlDoc As XmlDocument
'Set the connection string
'This connection string uses integrated security to connect to the
'TWO database on the local computer
ConnectionString = "Data Source=localhost;Integrated Security=SSPI;" _
    & "Persist Security Info=False;Initial Catalog=TWO;"
'Load the contents of the textbox into the xmlDoc object
xmlDoc.LoadXml(XmlDoc_TextBox.Text)
Try
    'Instantiate an eConnectMethods object
    Dim eConnectObject As New eConnectMethods
    'If eConnectResult is TRUE, the XML document was successfully submitted
    eConnectResult = eConnectObject.CreateEntity(ConnectionString,
        xmlDoc.OuterXml)
'If an eConnect error occurs, display the error message
Catch eConnectError as eConnectException
    ReturnData_TextBox.Text = eConnectError.Message
'If an unexpected error occurs, display the error message
Catch ex As Exception
    ReturnData_TextBox.Text = ex.Message
End Try