DOCUMENTATION NTi

Executing a CL command

Introduction

This example shows how to implement a CL command call from NTi in C#.

Step 1/2: Creating and opening a connection

We start by declaring a new instance of NTiConnection and opening it:

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

Step 2/2: Call the command

From the open connection, you can then execute a CL command using the ExecuteClCommand() method of the NTiConnection class:

conn.ExecuteClCommand("CRLIB LIB(MYLIB) TEXT('My new library')");

In the event of an error in the execution of the command, an NTiException error is raised with a message detailing the problem, so we can implement a try ... catch ... to intercept any error:

try{
  conn.ExecuteClCommand("CRLIB LIB(MYLIB) TEXT('My new library')");
}
catch(Exception ex){
  Console.WriteLine(ex.Message);
}

In the event of an error, the message will be written to the console.

Code summary

The final code for executing a CL command from .NET with NTi - with basic error handling - is as follows:

//Opening the connection
var conn = new NTiConnection(connectionString);
conn.Open();

//CL command execution
try{
  conn.ExecuteClCommand("CRLIB LIB(MYLIB) TEXT('My new library')");
}
catch(Exception ex){
  Console.WriteLine(ex.Message);
}

//Closing the connection
conn.Close();

You should also remember to close the connection at the end of processing if you don't want to use it again. However, when the conn object is destroyed at the end of the program or when the current method is exited, the connection will automatically be closed.