Call IBM i (AS/400) Program in C# (.NET) with NTi

Introduction

This tutorial shows how to call an IBM i (AS/400) program from a C# (.NET) application using NTi Data Provider.

Calling an IBM i program from a .NET application lets you reuse existing business logic (RPG, CL) without rewriting it. This makes it possible to integrate existing RPG programs into modern applications without touching the legacy code.

With NTi, from C# code you can:

  • define input, output and input/output parameters
  • call an IBM i program with CallProgram
  • retrieve the returned data

Program and parameter description

The program MYPGM from library MYLIB expects the following parameters:

Description Type Direction Value
Text 1 CHAR(10) Input Hello
Text 2 CHAR(10) Input World
Start position BYTE(1) Input 0x00
Return variable length BYTE(4) Input 128
Return variable CHAR(*) Output vide
Error code CHAR(50) InputOutput vide

The return variable is structured as follows:

Offset Length Description
0 64 Message 1
64 64 Message 2

L'appel serait réalisé avec le code suivant:

Step 1 - Open the connection

Declare a NTiConnection instance and open the connection:

using var conn = new NTiConnection("server=serverName;user=userName;password=password");
conn.Open();

Step 2 - Create the parameters

Referring to the parameter description above, create the parameter list with their values:

List<NTiProgramParameter>  parms = new List<NTiProgramParameter> () {
   new NTiProgramParameter("Hello", 10).AsInput(),     // CHAR(10) INPUT
    new NTiProgramParameter("World", 10).AsInput(),     // CHAR(10) INPUT
    new NTiProgramParameter(new byte[] {0x00}).AsInput(),     // BYTE(1)  INPUT
    new NTiProgramParameter(128).AsInput(),     // BYTE(4)  INPUT
    new NTiProgramParameter("", 128).AsOutput(),     // CHAR(128) OUTPUT
    new NTiProgramParameter("", 50)     // CHAR(50)  I/O
};

Step 3 - Call the program

Call the program using the CallProgram() method of NTiConnection :

conn.CallProgram("MYLIB", "MYPGM", parms);

Step 4 - Retrieve the returned data

Once the program has been called, retrieve the data from the return variable (parameter #5):

string message1 = parms[4].GetString(0, 64);
string message2 = parms[4].GetString(64, 64);

Summary

Complete code to call an IBM i program from .NET with NTi:

using var conn = new NTiConnection("server=serverName;user=userName;password=password");
conn.Open();

var parms = new List
{
    new NTiProgramParameter("Hello", 10).AsInput(),      
    new NTiProgramParameter("World", 10).AsInput(),   
    new NTiProgramParameter(new byte[] {0x00}).AsInput(), 
    new NTiProgramParameter(128).AsInput(),               
    new NTiProgramParameter("", 128).AsOutput(),        
    new NTiProgramParameter("", 50)                     
};

conn.CallProgram("MYLIB", "MYPGM", parms);

string message1 = parms[4].GetString(0, 64);
string message2 = parms[4].GetString(64, 64);

What's next?