Calling stored procedure on i Series using IBM DB2 Connect driver

9.6k Views Asked by At

I have a C# application, from where I am trying to call an ISeries Stored Procedure (wrapping an RPGLE program). This RPGLE program returns a result set back to my C# application. When I try to call the stored procedure, it errors and hangs indefinitely. In the debug mode, I can see the SQL error message as shown at the end of the post. If I use OleDBConnection it works fine, but it doesn't work with DB2 Connect.

I have tried to setup Tracing on my page, but it didn't help as the page just hangs and I am not able to view the stack trace.

On ISeries side, I can see that the program executes correctly (For testing, I am writing a record in a test file at the end of the program.)

Has anybody else faced this error before? I would appreciate any help on this.

Thanks.

Code:-

DB2Connection conn1 = new DB2Connection(ConfigurationManager.ConnectionStrings  ["db2IBM"].ConnectionString);
        conn1.Open();

        string callString5 = "PGMLIBLE.GETCONSIGNMENTS";

        DB2Command cmd1 = new DB2Command(callString5, conn1);
        cmd1.CommandType = CommandType.StoredProcedure;
        cmd1.CommandTimeout = 5;

        DB2Parameter prmCons = cmd1.Parameters.Add("prmCons", DB2Type.Char, 7);
        prmCons.Direction = ParameterDirection.Input;
        prmCons.Value = "JUT0016";

        DB2Parameter prmCmp = cmd1.Parameters.Add("prmCmp", DB2Type.Char, 3);
        prmCmp.Direction = ParameterDirection.Input;
        prmCmp.Value = "DTA";

        DB2Parameter prmIorE = cmd1.Parameters.Add("prmIorE", DB2Type.Char, 1);
        prmIorE.Direction = ParameterDirection.Input;
        prmIorE.Value = "Y";

        DB2DataAdapter adp = new DB2DataAdapter(cmd1);
        DataTable dt = new DataTable();

        adp.Fill(dt);
     // If I just execute cmd1.executeNonquery it doesn't error.
     //  cmd1.ExecuteNonQuery(); 
        GridView2.DataSource = dt;
        GridView2.DataBind();

        conn1.Close();
        conn1.Dispose();
        cmd1.Dispose();

Error Message:-

ERROR [58005] [IBM][DB2.NET] SQL0902 An unexpected exception has occurred in  Process: 5652 Thread 10 AppDomain: Name:aa196883-1-129406018469512609
There are no context policies.

Function: SQLExecDirectADONET (Params)
 CallStack:    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at IBM.Data.DB2.DB2ConnPool.HandleUnknownErrors(String strFncMsg, Exception exception, Boolean bThrow)
   at IBM.Data.DB2.DB2Command.ExecuteReaderObject(CommandBehavior behavior, String method, DB2CursorType reqCursorType, Boolean abortOnOptValueChg, Boolean skipDeleted, Boolean isResultSet, Int32 maxRows, Boolean skipInitialValidation)
   at IBM.Data.DB2.DB2Command.ExecuteReaderObject(CommandBehavior behavior, String method)
   at IBM.Data.DB2.DB2Command.ExecuteReader(CommandBehavior behavior)
   at IBM.Data.DB2.DB2Command.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at ShowConsignmentsOpen.Page_Load(Object sender, EventArgs e) in c:\Inetpub\TestStoredProc\ShowConsignmentsOpen.aspx.cs:line 50
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.showconsignmentsopen_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\teststoredproc\f35f719b\99c08192\App_Web_yti93vc5.2.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
   at System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(Exception error)
   at System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr)
   at System.Web.HttpRuntime.ProcessRequestNoDemand(HttpWorkerRequest wr)
   at System.Web.HttpRuntime.ProcessRequest(HttpWorkerRequest wr)
   at Microsoft.VisualStudio.WebHost.Request.Process()
   at Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Connection conn) InnerException Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Check InnerException property for more detail.  SQLSTATE=58005
2

There are 2 best solutions below

2
On

I have always done the following:

    DB2Connection conn1 = new DB2Connection(ConfigurationManager.ConnectionStrings ["db2IBM"].ConnectionString);
    conn1.Open();

    string callString5 = "PGMLIBLE.GETCONSIGNMENTS";

    DB2Command cmd1 = new DB2Command(callString5, conn1);
    cmd1.CommandType = CommandType.StoredProcedure;
    cmd1.CommandTimeout = 5;

    cmd1.Parameters.Add("prmCons", DB2Type.Char, 7).Value = "JUT0016";
    prmCons.Direction = ParameterDirection.Input;

    cmd1.Parameters.Add("prmCmp", DB2Type.Char, 3).Value = "DTA";
    prmCmp.Direction = ParameterDirection.Input;

    cmd1.Parameters.Add("prmIorE", DB2Type.Char, 1).Value = "Y";
    prmIorE.Direction = ParameterDirection.Input;

    DB2DataAdapter adp = new DB2DataAdapter(cmd1);
    DataTable dt = new DataTable();

    adp.Fill(dt);
 // If I just execute cmd1.executeNonquery it doesn't error.
 //  cmd1.ExecuteNonQuery(); 
    GridView2.DataSource = dt;
    GridView2.DataBind();

    conn1.Close();
    conn1.Dispose();
    cmd1.Dispose();

I can't remember how to do the direction, but this is basically how I have implemented this in the past.

0
On

This was asked long ago, but I'll answer it anyway.

Take a look at this error message:

ERROR [58005] [IBM][DB2.NET] SQL0902 An unexpected exception has occurred in Process: 5652 Thread 10 AppDomain: Name:aa196883-1-129406018469512609
There are no context policies.

After doing a search on the text "There are no context policies.", I found this:

MSDN AppDomain.ToString Method

There, you will see the following:

Return Value
Type: System.String
A string formed by concatenating the literal string "Name:", the friendly name of the application domain, and either string representations of the context policies or the string "There are no context policies."

So, what does this mean?

Most likely, it comes from this:

ConfigurationManager.ConnectionStrings  ["db2IBM"].ConnectionString

That is exactly how you wrote it.

That error could be caused by either of these:

  1. those spaces are causing the error, or
  2. db2IBM does not exist in your Web.config file's Connection String section.

For help with setting up your Connection String section, see the following:

Setting up connection string in ASP.NET to SQL SERVER