Procedure for Listening for Events in VB.NET


Take the following steps to create a program that listens for event notifications from FREDI.

  1. Declare ediDocument object with WithEvents.
  2. Create an instance of ediDocument object, and apply environment settings.
  3. Load EDI document.
  4. Add event handler.
  5. Sample

 

Example in Visual Basic

This example will use the following:

In this example, the program is created to listen for event notifications returned from FREDI ActiveX/COM when it reads an EDI file.  The visual basic program contains a simple form having a command button to start the program execution, and a text control to display the event notifications.

 

Declare ediDocument object with WithEvents.  Declare ediDocument as object that is an event source.  The WithEvents keyword is used in the declaration as follows:

' Declare FREDI as event source.
Private WithEvents m_oEdiDoc As ediDocument

Create an instance of ediDocument object, and apply environment settings.  The ediDocument object is the top level application instance for Framework EDI (FREDI).  This object is the topmost object in the object model hierarchy of FREDI.  This instance is created at the top of the form and the variable is a class member of the form.

Dim oSchemas As ediSchemas
Dim oSchema As ediSchema

' Instance of FREDI as ediDocument.
m_oEdiDoc = New ediDocument

' To traverse all the data segments in the document at the best possible speed, enable Cursor_ForwardOnly.
m_oEdiDoc.CursorType = Cursor_ForwardOnly

' Disable standard reference library.
oSchemas = m_oEdiDoc.GetSchemas
oSchemas.EnableStandardReference = False

' Load implementation guideline used by the EDI document.
oSchema = m_oEdiDoc.LoadSchema(AppDomain.CurrentDomain.BaseDirectory & "..\SampleSefX12_850.SEF", SchemaTypeIDConstants.Schema_Standard_Exchange_Format)

Load EDI document.  Load the EDI document to open and begin file traversal.  When loaded successfully, traverse the file by stepping to each data segments in the EDI document.

' Load EDI document via the mailDocument object.
m_oEdiDoc.Load(AppDomain.CurrentDomain.BaseDirectory & "..\SampleEdiX12_850.X12")

' The EDI data is now made available in the ediDocument object.  
Dim oSegment As ediDataSegment

' Traverse all the data segments in the document.
oSegment = m_oEdiDoc.FirstDataSegment

While Not oSegment Is Nothing

' TODO: Do something with oSegment here.

' Use the .Set method to let FREDI know that the client no longer
' needs the old data in oSegment. The new data returned
' by oSegment.Next will replace old data in oSegment.
ediDataSegment.Set(oSegment, oSegment.Next())
Wend

MsgBox "Done"

Add event handler.  By declaring the ediDocument as an event source using WithEvents, the variable m_oEdiDoc variable will appear on the drop-down list to the left of the editor.

 

Select the m_oEdiDoc variable and the drop-down list to the right of the editor will display all the events available to the ediDocument class.

 

Select the EventNotify event and the m_oEdiDoc_EventNotify event procedure is automatically added to the form.  Add the following code to display the events in the text control Text1.

Private Sub m_oEdiDoc_EventNotify(ByVal EventNotify As Edidev.FrameworkEDI.ediEventNotify) Handles m_oEdiDoc.EventNotify

If EventNotify.Severity = EventIDConstants.Event_Info_InterchangeStart Then
TextBox1.Text = TextBox1.Text & "Start of Interchange. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLf
Application.DoEvents()
ElseIf EventNotify.Severity = EventIDConstants.Event_Info_InterchangeEnd Then
TextBox1.Text = TextBox1.Text & "End of Interchange. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLf
Application.DoEvents()
ElseIf EventNotify.Severity = EventIDConstants.Event_Info_FunctionalGroupStart Then
TextBox1.Text = TextBox1.Text & "Start of Functional Group. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLf
Application.DoEvents()
ElseIf EventNotify.Severity = EventIDConstants.Event_Info_FunctionalGroupEnd Then
TextBox1.Text = TextBox1.Text & "End of Functional Group. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLf
Application.DoEvents()
ElseIf EventNotify.Severity = EventIDConstants.Event_Info_TransactionSetStart Then
TextBox1.Text = TextBox1.Text & "Start of Transaction Set/Message. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLf
Application.DoEvents()
ElseIf EventNotify.Severity = EventIDConstants.Event_Info_TransactionSetEnd Then
TextBox1.Text = TextBox1.Text & "End of Transaction Set/Message. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLf
Application.DoEvents()
ElseIf EventNotify.Severity = EventIDConstants.Event_Notify Then
' Any general notifications sent by FREDI is captured here.
TextBox1.Text = TextBox1.Text & "Code: " & CStr(EventNotify.ErrorCode) & ", Notification: " & EventNotify.ErrorDescription & vbCrLf
Application.DoEvents()
ElseIf EventNotify.Severity = EventIDConstants.Event_Warning Then
' Any warnings sent by FREDI is captured here.
TextBox1.Text = TextBox1.Text & "Warning Code: " & CStr(EventNotify.ErrorCode) & ", Warning Description: " & EventNotify.ErrorDescription & vbCrLf
Application.DoEvents()
ElseIf EventNotify.Severity = EventIDConstants.Event_Severe Then
' Any severe errors sent by FREDI is captured here.
TextBox1.Text = TextBox1.Text & "Severe Code: " & CStr(EventNotify.ErrorCode) & ", Severe Description: " & EventNotify.ErrorDescription & vbCrLf
Application.DoEvents()
End If
End Sub

Select the EventNotifyInfo event and the m_oEdiDoc_EventNotifyInfo event procedure is automatically added to the form.  Add the following code to display the events in the text control Text1.

 

Private Sub m_oEdiDoc_EventNotifyInfo(ByVal EventNotifyInfo As Edidev.FrameworkEDI.ediEventNotifyInfo) Handles m_oEdiDoc.EventNotifyInfo

If EventNotifyInfo.EventInfoType = EventIDConstants.Event_Info_Error_Segment_Handle Then
' Supplemental error has been passed. In this case, the error
' generated has happened in the following segment handle.
TextBox1.Text = TextBox1.Text & "Segment Handle: " & CStr(EventNotifyInfo.NumericInfo) & vbCrLf

' The following error code ties this supplemental error to an
' error generated by EventNotify
TextBox1.Text = TextBox1.Text & "Error Code: " & EventNotifyInfo.StringInfo & vbCrLf
Application.DoEvents()
ElseIf EventNotifyInfo.EventInfoType = EventIDConstants.Event_Info_Error_Element_Position Then
' Supplemental error has been passed. In this case, the error
' generated has happened in the following data element in
' a segment having the following data segment handle.
TextBox1.Text = TextBox1.Text & "Segment Handle: " & CStr(EventNotifyInfo.NumericInfo) & vbCrLf

' Data element position in error.
TextBox1.Text = TextBox1.Text & "Element Position: " & EventNotifyInfo.StringInfo & vbCrLf
Application.DoEvents()
ElseIf EventNotifyInfo.EventInfoType = EventIDConstants.Event_Info_Error_Composite_Position Then
' Supplemental error has been passed. In this case, the error
' generated has happened in a sub element belong to a composite
' of a segment having the following segment handle.
TextBox1.Text = TextBox1.Text & "Segment Handle: " & CStr(EventNotifyInfo.NumericInfo) & vbCrLf

' Data element and sub element position in error.
TextBox1.Text = TextBox1.Text & "Component Element Position: " & EventNotifyInfo.StringInfo & vbCrLf
Application.DoEvents()
End If
End Sub

Select the DllNotifyInfo event and the m_oEdiDoc_DllNotifyInfo event procedure is automatically added to the form.  Add the following code to display the events in the text control Text1.

 

Private Sub m_oEdiDoc_DllNotifyInfo(ByVal DllNotify As Edidev.FrameworkEDI.ediDllNotifyInfo) Handles m_oEdiDoc.DllNotifyInfo

' DLL notifications are captured here.
TextBox1.Text = TextBox1.Text & "DLL Notify. DLL File Name: " & DllNotify.DllFileName & vbCrLf
TextBox1.Text = TextBox1.Text & "DLL Notify. Handle: " & CStr(DllNotify.Handle) & vbCrLf
TextBox1.Text = TextBox1.Text & "DLL Notify. String Parameter: " & DllNotify.StringParam & vbCrLf
TextBox1.Text = TextBox1.Text & "DLL Notify. Numeric Parameter: " & CStr(DllNotify.NumParam) & vbCrLf
Application.DoEvents()
End Sub

Select the ProgressNotify event and the m_oEdiDoc_ProgressNotify event procedure is automatically added to the form.  Add the following code to display the events in the text control Text1.

 

Private Sub m_oEdiDoc_ProgressNotify(ByVal ProgressNotify As Edidev.FrameworkEDI.ediProgressNotify) Handles m_oEdiDoc.ProgressNotify

If ProgressNotify.ProgressState = ProgressStateConstants.Progress_Phase_Start Then
Select Case ProgressNotify.ProgressType
Case ProgressTypeConstants.Progress_Type_Single_Import
TextBox1.Text = TextBox1.Text & "Progress Start. Importing single SEF" & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Codes_Read
TextBox1.Text = TextBox1.Text & "Progress Start. Reading SEF Codes" & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Document_Read
TextBox1.Text = TextBox1.Text & "Progress Start. Reading EDI Document" & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Document_Save
TextBox1.Text = TextBox1.Text & "Progress Start. Saving EDI Document" & vbCrLf
Application.DoEvents()
End Select
ElseIf ProgressNotify.ProgressState = ProgressStateConstants.Progress_Phase_Update Then
Select Case ProgressNotify.ProgressType
Case ProgressTypeConstants.Progress_Type_Single_Import
TextBox1.Text = TextBox1.Text & "Progress Update. Importing single SEF at %" & CStr(ProgressNotify.Percent) & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Codes_Read
TextBox1.Text = TextBox1.Text & "Progress Update. Reading SEF Codes at %" & CStr(ProgressNotify.Percent) & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Document_Read
TextBox1.Text = TextBox1.Text & "Progress Update. Reading EDI Document at %" & CStr(ProgressNotify.Percent) & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Document_Save
TextBox1.Text = TextBox1.Text & "Progress Update. Saving EDI Document at %" & CStr(ProgressNotify.Percent) & vbCrLf
Application.DoEvents()
End Select
ElseIf ProgressNotify.ProgressState = ProgressStateConstants.Progress_Phase_Complete Then
Select Case ProgressNotify.ProgressType
Case ProgressTypeConstants.Progress_Type_Single_Import
TextBox1.Text = TextBox1.Text & "Progress Completed. Importing single SEF" & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Codes_Read
TextBox1.Text = TextBox1.Text & "Progress Completed. Reading SEF Codes" & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Document_Read
TextBox1.Text = TextBox1.Text & "Progress Completed. Reading EDI Document" & vbCrLf
Application.DoEvents()
Case ProgressTypeConstants.Progress_Type_Document_Save
TextBox1.Text = TextBox1.Text & "Progress Completed. Saving EDI Document" & vbCrLf
Application.DoEvents()
End Select
End If
End Sub