Learning never exhausts the mind
Home >  Coding > C# ASP.Net MVC > Calling IBM iSeries RPG Programs with SQL Stored Procedures and C#

Published 26th May 2010 by

In this tutorial, we will look at calling IBM iSeries RPG programs with SQL stored procedures. I have found this method to be the best, both in terms of flexibility, scalability and performance.

When I first started working with ASP.Net and an iSeries back end I used the IBM CWBX API Calls to communicate with the existing business logic, however, I soon discovered that performance was going to be an issue and decided to try a different solution. Calling RPG programs with SQL stored procedures also adds an additional security layer to the back end. Since the front end cannot specify or change what program is called, the only commands that can be executed are those defined in the stored procedure.

The RPG programs on the iSeries do not need to be changed as long as they support in and out parameters, the same as those using iSeries CWBX API calls. The only changes that need to be done on the iSeries are the addition of stored procedures into a library.

Creating a SQL stored procedure on an iSeries (for those who don't already know) is as simple as running the following command within the SQL environment.


Where LIBRARY/PROCNAME is the location and name of the stored procedure and RPGOBJ/RPGPRG is the library and name of the program. In this example, there is one parameter defined with a length of 2500 characters. You can prompt the command for further details and help as I am not very familiar with the iSeries SQL parameters.

On the .Net side we need to use the IBM DB2 Provider and OleDB. This method works in very much the same way as calling a Microsoft SQL Server stored procedure, with the exception of a few different objects.

Let's have a look at a quick example:

string result = string.Empty;
string conStr = "DataSource=;DefaultCollection=LIBRARY;UserID=USERNAME;Password=PASSWORD; DataCompression=True;Pooling=true;"; // Change to suit your system

// Create a connection
iDB2Connection conn = null;

  conn = new iDB2Connection(conStr);

  // Create the command
  iDB2Command dbCmd = new iDB2Command();
  dbCmd.CommandText = "PROCNAME"; // Name of the stored procedure
  dbCmd.CommandType = CommandType.StoredProcedure;
  dbCmd.Connection = conn;

  // Add a parameter
  iDB2Parameter dbParam_ID = new iDB2Parameter();
  dbParam_ID.ParameterName = "@parameter1";
  dbParam_ID.Value = "Example";
  dbParam_ID.DbType = DbType.String;
  dbParam_ID.Direction = ParameterDirection.InputOutput;

  // Call the procedure

  // Get the response
  result = dbParam_ID.Value.ToString().Trim();
  // Do your error handling here
  // Close the connection
  if (conn != null)


Note: Do not forget to trim the result coming back from the stored procedure. If the procedure defines a length of 2500 characters, you will get back 2500 characters even if the data is only 1 character long.

The performance of this method is (in my opinion) much greater than that of the CWBX API calls and because it uses native SQL calls it is far more scalable. The only thing I do with my code is I have a managed connection pool as it takes a few seconds to create a new iDB2Connection object.

RGP Code

Here's a small example RPG program which shows how to read and write to the parameters. This program will convert an incoming parameter string from lower to upper case.

d* Constants                                                         *   
d c#Lower         c                   Const('abcdefghijklmnopqrstuvwxyz')
d c#Upper         c                   Const('ABCDEFGHIJKLMNOPRRSTUVWXYZ') 

 * Program Variables                                                 * 
d InParms         s           2500a

 * Entry parms                                                       * 
 * Entry parameters                                                    
c     *Entry        Plist                                              
c                   Parm                    InParms                    

 * Mainline                                                         *
 * Convert the incoming parameter from Lower to Upper case                                  
c                   Eval      InParms = %XLate(c#Lower:c#Upper:InParms)
 * End the program (set on the Last Record indicator and Return to the calling program)
c                   Eval      *INLR = *On                                              
c                   Return                                                             =
One thought on “Calling IBM iSeries RPG Programs with SQL Stored Procedures and C#
  • 23rd April 2014 at 12:00 am

    Having issues with loading a data grid with data set coming from RPG stored procedure. Do you have sample? I'm using C# and VSP2013. Thanks.


Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.