Tutorials

Call the IBM i QSYS.QCMDCHK API from .NET with NTi

ByRémi Rouillot

Illustration for the article

Detailed content of the article:Call the IBM i QSYS.QCMDCHK API from .NET with NTi

IBM i APIs offer a wide range of features for system administration and spool file management. This article shows how to call the QCMDCHK API from .NET with NTi Data Provider, in just a few lines of C#.

The QSYS.QCMDCHK API

The QCMDCHK API from the QSYS library validates the syntax and correctness of a CL command before execution. Its IBM documentation specifies two input parameters:

Parameter IBM i type Direction Role
Command string Char(*) I/O command to check
Length of command string Packed(15,5) Input length in number of characters

Call QSYS.QCMDCHK from .NET with NTi

In this example, the API is called to validate the following command:

CRTLIB CLIENTS

Step 1 - Establish the connection

var conn = new NTiConnection(connectionString);
conn.Open();

Step 2 - Declare the parameters

var cmd = "CRTLIB CLIENTS";          
var parms = new List<NTiProgramParameter>()
{
    new NTiProgramParameter(cmd, cmd.Length),
    new NTiProgramParameter(cmd.Length, 15, 5)
};

NTi supports all parameter types: integers, packed decimals, character strings.

Step 3 - Call the API

conn.CallProgram("QSYS", "QCMDCHK", parms);

Summary

The QCMDCHK call is achieved with the following code:

var conn = new NTiConnection(connectionString);
conn.Open();

var cmd = "CRTLIB CLIENTS";
var parms = new List<NTiProgramParameter>()
{
    new NTiProgramParameter(cmd, cmd.Length),
    new NTiProgramParameter(cmd.Length, 15, 5)
};

conn.CallProgram("QSYS", "QCMDCHK", parms);

Two possible outcomes at runtime:

Valid command - no exception thrown, return code is 0.

Invalid command - an exception is raised on the C# side with a CPFXXXX code and its explanation.

Conclusion

With NTi Data Provider, call an IBM i system API takes just a few lines of C#, with no green screen involved. By combining system APIs, CL commands and SQL services, IBM i administration tasks can be automated and integrated into modern interfaces.

This example uses two parameters, but the same principle applies regardless of the complexity of the API being called..


Rémi Rouillot

Ready to get started?

Get your free trial license online
and connect your .NET apps to your IBM i right away.

Create your account

Log in to the Aumerial portal, generate your trial license and activate NTi on your IBM i instantly.

Start your trial

Add NTi to your project

Install NTi Data Provider from NuGet in Visual Studio and reference it in your .NET project.

View documentation

Need help?

If you have questions about our tools or licensing options, our team is here to help.

Contact us
30-day free trial instant activation no commitment nothing to install on the IBM i side