Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi experts.
My aim is to call through ASP a CL program (in AS400) and return the result.
I have fount a nice article about this at Calling AS/400 (AS400) RPG Programs From ASP.NET[^] . But it does not elaborates anything regarding CL program.

As given in the article, I have created the class AS400Program.cs,


C#
using System;

namespace Interfaces.Source
{
    /// <summary>
    /// Summary description for AS400Program.
    /// </summary>
    public class AS400Program
    {
        private bool as400Configured = false;
        private cwbx.AS400System as400;
        private cwbx.Program program;

        // configuration format:
        // as400;userid;password;library;program
        public AS400Program(string configuration)
        {
            if(!as400Configured)
            {
                string[] settings = configuration.Split(';');
                if(settings.Length == 5)
                {
                    as400 = new cwbx.AS400SystemClass();
                    program = new cwbx.Program();

                    as400.Define(settings[0]);
                    program.system = as400;
                    program.system.UserID = settings[1];
                    program.system.Password = settings[2];
                    program.LibraryName = settings[3];
                    program.ProgramName = settings[4];
                    as400Configured = true;
                }
                else
                {
                    throw(new Exception(
                        string.Format("Invalid AS400Program configuration string : [{0}]", configuration)));
                }
            }
        }

        public bool Invoke(bool throwInsteadOfReturn, ref cwbx.ProgramParameters parameters)
        {
            bool success = false;

            try
            {
                if(as400.IsConnected(cwbx.cwbcoServiceEnum.cwbcoServiceRemoteCmd) == 0)
                {
                    //  Lost connection with the AS400.  Disconnect, then reconnect.
                    as400.Disconnect(cwbx.cwbcoServiceEnum.cwbcoServiceAll);
                    as400.Connect(cwbx.cwbcoServiceEnum.cwbcoServiceRemoteCmd);

                    if(as400.IsConnected(cwbx.cwbcoServiceEnum.cwbcoServiceRemoteCmd) == 0)
                    {
                        //  Log something here.
                    }
                }

                program.Call(parameters);
                success = true;
            }
            catch(Exception e)
            {
                // Capture to log the errors but then re-throw it to let caller know there was trouble.
                if(as400.Errors.Count > 0)
                {
                    foreach(cwbx.Error error in as400.Errors)
                    {
                        //  Log something here.
                    }
                }

                if(program.Errors.Count > 0)
                {
                    foreach(cwbx.Error error in program.Errors)
                    {
                        //  Log something here.
                    }
                }

                if(throwInsteadOfReturn)
                {
                    throw(e);
                }
            }

            return(success);
        }

        public void Close()
        {
            as400.Disconnect(cwbx.cwbcoServiceEnum.cwbcoServiceAll);
        }
    }
}


And the function similarly:

C#
//  Assume the RPG program being called takes one input paramater, PartId, and returns the PartPrice.

protected enum DataLengths : int
{
    PartId     = 12,
    PartPrice  = 15,
}


string partId = "3001891A";  // hardcoded for this example


AS400Program program = new AS400Program(ConfigurationSettings.AppSettings["partsPricingConfig"]);


cwbx.StringConverter stringConverter = new cwbx.StringConverterClass();
cwbx.PackedConverter packedConverter = new cwbx.PackedConverterClass();
packedConverter.DecimalPosition = 4;
packedConverter.Digits = (int)DataLengths.PartPrice;

cwbx.ProgramParameters parameters = new cwbx.ProgramParametersClass();

parameters.Append("PartId", cwbx.cwbrcParameterTypeEnum.cwbrcInout, (int)DataLengths.PartId);
stringConverter.Length = (int)DataLengths.PartId;
parameters["PartId"].Value = stringConverter.ToBytes(partId.PadRight((int)DataLengths.PartId, ' '));

parameters.Append("PartPrice", cwbx.cwbrcParameterTypeEnum.cwbrcInout, (int)DataLengths.PartPrice);
parameters["PartPrice"].Value = packedConverter.ToBytes("0");

program.Invoke(true, ref parameters);

string price = packedConverter.FromBytes(parameters["PartPrice"].Value);

program.Close();

return(Double.Parse(price));


I need to know where to call the CL program, and use the price value returned from the function.
Any info regarding this will be appreciated. :)
Posted

It's been far too long since I counted RPG and CL as my friends - but I found this post which shows how to call a CL program with parameters successfully...

Not sure if it will help [^]
 
Share this answer
 
Comments
ujju.1 30-Jan-13 1:56am    
Thanx Max. I am trying that now.
Got it.
C#
program.Invoke(true, ref parameters);
invokes the program that we declare at appsettings (read connection from
C++
AS400Program(ConfigurationSettings.AppSettings["partsPricingConfig"]);
)
 
Share this answer
 
AS400 is very old technology and I don't have much idea about it. But after searching I came across the following articles. You can check it. This may help you.
http://www.netsplore.com/PublicPortal/Default.aspx?tabid=246[^]
AS400 Data Connection in ASP.NET[^]


--Amit
 
Share this answer
 
Comments
Kiko.17 6-Jun-13 7:36am    
Hi,

Just want to know which is faster.
Calling RPG program or using odbc?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900