Skip to main content

Problem:

How can you access an App.config file from COBOL in .Net ?

For example you may want to store database connection details in an XML Config file and retrieve them at runtime.

Resolution:

A demo of how to access an XML Config files is attached to this.

Some example code to do this is:-

       $set sourceformat"variable"

       program-id. ConfigReader.

      *****************************************************

      *

      *     Author  - David Sands (Micro Focus Technical Support EMEA)

      *

      *     Purpose - Show how to access appSettings config data that is

      *               help within the application config file.

      *              

      *****************************************************

       environment division.

       configuration section.

       repository.

            class cAppSettingsReader as "System.Configuration.AppSettingsReader"

            class cString as "System.String"

            class cInvalidOperationException as "System.InvalidOperationException"

            .

       data division.

       working-storage section.

       01  myReader     object reference cAppSettingsReader.

       

       01  setting1     pic x(30).

       01  setting2     pic x(30).

       

       01  ws-invalidop object reference cInvalidOperationException.

       

       procedure division.

       

            display "About to Read App Settings...."

            

            invoke cAppSettingsReader::"New" returning myReader

            

            set setting1 to myReader::"GetValue"("MFTEST1",type of cString) as cString  *> "as" used to cast object as a string type

            display "MFTEST1=" setting1

            set setting2 to myReader::"GetValue"("MFTEST2",type of cString) as cString

            display "MFTEST2=" setting2

      *

      ***** Use try catch block to detect when a setting is not found.

      *     

            try

                set setting2 to myReader::"GetValue"("MFxxTEST2x",type of cString) as cString

            catch ws-invalidop            

               display ws-invalidop::"Message"

            end-try

            stop run.

Attachments:

XMLConfigReader.zip

Old KB# 3853