Calling IBM i RPG programs in C# with NTi

Quentin Destrade

Friday 09 August 2024

  • Tutorials

I'm going to show you how easy it is to call an existing RPG program on IBM i with NTi. This approach makes it possible to modernise and integrate legacy systems into modern environments such as .NET, while preserving the quality of existing services and applications.

IBM i News

Introduction

RPG programs on IBM i remain at the heart of many enterprise management systems, providing a solid and reliable foundation for mission-critical operations. Faced with pressure to modernise and evolve their infrastructures, many companies have attempted to replace this legacy, and many of these projects have come up against major obstacles: the complexity of the task, a vast volume of undocumented code, and above all a significant technical debt. More often than not, these efforts have ended up as isoperimeter applications, with no real progress being made, or as costly and uncertain ERP integration projects.

NTi offers an ideal solution for preserving and enhancing this heritage by integrating existing RPG programmes into modern environments such as .NET. With just a few lines of code, it's possible to make the RPG work with C#, enabling a smooth evolution towards more modern technologies, while preserving what has proved its worth.

Scenario: Updating a first name in the NORTHWIND database

We are going to see how to call from .NET, an existing RPG program that updates a customer name in the NORTHWIND database from a C# console application.

This is a very simple example where we will enter the customer ID and the new name to be added as input parameters. This information will then be used by the RPG program to update the customer name in the database.

I already have an application that displays the CUSTOMER table records in NORTHWIND in table form.

At the moment the "name" field linked to the "ALFKI" ID is empty:

article 10

Step 1: Check the parameters expected by the RPG program

First of all, we check the parameters expected by the program by taking a DUMP when it is executed.

article 10

The DUMP indicates that two parameters are required.

Step 2: Establishing the NTi connection

On the C# side, I open a console project in which I create an object of type NTiConnection which will allow us to interact with our IBM i from our application.

string conn = "server=***.***.**.**;user=***************; Password=******************************;";
var _conn = new NTiConnection(conn);
_conn.Open();
Console.WriteLine("Connection established");

Step 3: Declaration of parameters

The RPG program PGMCUST01 expects two parameters (seen in step 1).

  • Customer ID : 5 character positions.
  • The new name: 30 character positions.

We prepare these parameters by creating a list of objects of type NTIProgramParameter.

We first retrieve the customer ID and the new first name via the console, then add them to the list of parameters that will be passed to the RPG program.

Console.WriteLine($"Enter the customer ID");
var customerId = Console.ReadLine();

 Console.WriteLine($"Enter the new name:");
 var newName = Console.ReadLine();

 var parms = new List<NTiProgramParameter>()
 {
     new NTiProgramParameter(customerId, 5),
     new NTiProgramParameter(newName, 30 )
 };

Step 4: Prepare the runtime environment and call the RPG program

First, initialise the current library, in this case the "NORTHWIND" library, using the CHGCURLIB NORTHWIND command.

 _conn.ExecuteClCommand("CHGCURLIB NORTHWIND");

💡Note: If the libraries required to run the program have been defined on the IBM i in the management of the profile used for the connection, the "CHGCURLIB" and "ADDLIBLE" commands are not mandatory.

Once the parameters have been defined, we call the PGMCUST01 program using NTi.

  _conn.CallProgram("NORTHWIND", "PGMCUST01", parms);

💡Note: You could have avoided specifying the "NORTHWIND" runtime library and replaced it with "*LIBL" or "*CURLIB".

This is where NTi differs from other products, in that the session opened in the C# code is comparable to that of a 5250 screen.

Step 5: Validating the results

After running the program, the corresponding field in the NORTHWIND database is updated with the new name entered. We can visually check the result in the console, and the changes live on a front-end application:

using Aumerial.Data.Nti;

class Program
{
    static void Main()
    {
        // Connection

        string conn = "server=*********;user=********; Password=*************;";
        
        var _conn = new NTiConnection(conn);
        _conn.Open();
        Console.WriteLine("connection established");

        Console.WriteLine($"Enter the customer ID");
        var customerId = Console.ReadLine();

        Console.WriteLine($"Enter the new name:");
        var newName = Console.ReadLine();

        
        var parms = new List<NTiProgramParameter>()
        {
            new NTiProgramParameter(customerId, 5),
            new NTiProgramParameter(newName, 30 )
        };


        _conn.ExecuteClCommand("CHGCURLIB NORTHWIND");
 
        _conn.CallProgram("NORTHWIND", "PGMCUST01", parms);

        string result = parms[1].GetString();

        Console.WriteLine($"customer name: {result}");
    }
}

article 10

article 10

In a nutshell

It's possible to retain the tried and tested legacy applications that form the unique identity and expertise of your core business, while integrating them into a modern IT environment.

This hybrid approach offers the best of both worlds: the reliability and robustness of IBM i systems combined with the flexibility and scalability of modern technologies such as .NET. RPG programs, as we call them here, can be run from containerised environments, such as Docker or Red Hat OpenShift, and used in all types of .NET applications.

The example presented here is simple, but it offers a glimpse of NTi's potential to create value around the resources generated by this business back office, ensuring that what works for you today will continue to do so tomorrow, with all the efficiency you expect.

Don't hesitate to contact us to try out NTi!

Quentin Destrade

Back