SAP.Middleware.Connector.RfcInvalidParameterException:表行0超出范围:表为空

问题描述

我有一个C#控制台应用程序,该应用程序连接到SAP服务器以从其某些表中获取数据。 有时,当我尝试将值从“ SOHEADER”的SAP表中的字段复制到字符串变量时,会弹出此错误。 这是堆栈跟踪:

SAP.Middleware.Connector.RfcInvalidParameterException:表行0 超出范围:表为空en SAP.Middleware.Connector.RfcTable.get_Item(Int32 lineIndex)zh-cn ExportarPedidosSAP.Program.ControlFacturasRemitosNotasCreditos(RfcDestination rfcDest,String和nroerror) C:\ Users \ USER \ Source \ repos \ BSCPY \ BSCPY \ ExportarPedidosSAP \ Program.cs:línea zh_cn ExportarPedidosSAP.Program.ConnSAPNotasCreditos()1220 zh_cn C:\ Users \ USER \ Source \ repos \ BSCPY \ BSCPY \ ExportarPedidosSAP \ Program.cs:línea 936

这是ControlFacturasRemitosNotasCreditos函数:

    static void ControlFacturasRemitosNotasCreditos(RfcDestination rfcDest,out string nroerror)
    {

        Console.WriteLine("**  Controlando remitos... ");
        nroerror = "";

        using (SqlBriefcaseCropScience db = new SqlBriefcaseCropScience())
        {
            using (var transaction = new TransactionScope(TransactionScopeOption.Required,new TransactionOptions { IsolationLevel = IsolationLevel.Snapshot }))
            {
                List<NotaCreditoCabecera> lstpedidos = new List<NotaCreditoCabecera>();
                List<NotaCreditoItem> lstItemProducto = new List<NotaCreditoItem>();
                IQueryable<NotaCreditoCabecera> ilstpedidos = null;
                IQueryable<Compania> ilstcompanias = null;
                List<NotaCreditoItem> lstprod = null;
                NotaCreditoItem itProd = null;
                int statuscode = 0;
                decimal FKIMG = 0;
                string ProdCode = "";
                string statusdesc = "";
                string status_doc = "";
                string LEGALNUMBER = "";
                string LEGALNUMBER_DEL = "";
                bool savecabecera = false;
                bool nolegalnumber = false;
                bool itemfacturado = false;
                bool pedidofacturado = false;
                List<string> List_LEGALNUMBER_DEL = new List<string>();
                List<string> List_LEGALNUMBER = new List<string>();

                ilstcompanias = db.Compania;

                DateTime unmes = Helpers.GetCurrentDateTime().AddMonths(1);

                if (ilstcompanias != null)
                {
                    foreach (Compania comp in ilstcompanias.ToList())
                    {
                        ilstpedidos = db.NotaCreditoCabecera.Where(c => c.CodigoCompania == comp.Codigo &&
                            (c.CodigoEstado == 5 || c.CodigoEstado == 11 && (DbFunctions.TruncateTime(c.FechaEstado) <= DbFunctions.TruncateTime(unmes))));

                        if (ilstpedidos != null)
                        {
                            lstpedidos = ilstpedidos.ToList<NotaCreditoCabecera>();
                            if (lstpedidos.Count() > 0)
                            {
                                RfcRepository rfcRep = rfcDest.Repository;
                                var funcionStatus = ConfigurationManager.AppSettings["SAP-RFC-Status"];

                                foreach (NotaCreditoCabecera cabecera in lstpedidos.ToList())
                                {
                                    lstprod = db.NotaCreditoItem.Where(c => c.CodigoCompania == comp.Codigo && c.NroPedido == cabecera.NroPedido).ToList();

                                    IRfcFunction functionStatus = rfcRep.CreateFunction(funcionStatus);
                                    functionStatus.SetValue("SEL_ORDERSTATUS","X");
                                    functionStatus.SetValue("P_VKORG",cabecera.SalesOrg);
                                    functionStatus.SetValue("P_BRIEFDOC",cabecera.NroPedido);

                                    IRfcTable tableHstatus = functionStatus.GetTable(ConfigurationManager.AppSettings["SAP-TablaH-Status"]);
                                    IRfcTable tableIstatus = functionStatus.GetTable(ConfigurationManager.AppSettings["SAP-TablaI-Status"]);
                                    IRfcTable tablaEstatus = functionStatus.GetTable(ConfigurationManager.AppSettings["SAP-TablaE"]);
                                    functionStatus.Invoke(rfcDest);

                                    status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();

                                    itemfacturado = false;
                                    pedidofacturado = false;
                                    nolegalnumber = false;

                                    if (tableIstatus.RowCount > 0)
                                    {
                                        for (int e = 0; e < tableIstatus.RowCount; e++)
                                        {
                                            if (Convert.ToString(tableIstatus[e].GetValue("NETWR")) != "")
                                            {
                                                ProdCode = Convert.ToString(tableIstatus[e].GetValue("MATERIAL"));
                                                itProd = lstprod.FirstOrDefault(i => i.CodigoProducto == ProdCode);

                                                if (itProd != null)
                                                {
                                                    //Num Factura
                                                    LEGALNUMBER = Convert.ToString(tableIstatus[e].GetValue("LEGALNUMBER")).Trim();
                                                    if (!String.IsNullOrEmpty(LEGALNUMBER))
                                                    {
                                                        itemfacturado = true;

                                                        //Num Remito
                                                        LEGALNUMBER_DEL = Convert.ToString(tableIstatus[e].GetValue("LEGALNUMBER_DEL")).Trim();

                                                        //Cantidad
                                                        FKIMG = (decimal.TryParse(Convert.ToString(tableIstatus[e].GetValue("FKIMG")),out FKIMG) == true) ?
                                                           Convert.ToDecimal(Convert.ToString(tableIstatus[e].GetValue("FKIMG"))) : 0;

                                                        //Si algun item no esta completamente facturado se considera el pedido no facturado
                                                        if ((LEGALNUMBER != "" && LEGALNUMBER_DEL != "") && (itProd.CantidadDevolucion == Convert.ToDecimal(FKIMG)) && (nolegalnumber == false))
                                                        {
                                                            pedidofacturado = true;
                                                        }
                                                        else
                                                        {
                                                            pedidofacturado = false;
                                                            break;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        nolegalnumber = true;
                                                    }
                                                }
                                            }
                                        }
                                        //statuscode = 0;
                                        //if (itemfacturado == true)
                                        //{
                                        //    if (pedidofacturado == true)
                                        //    {
                                        //        statuscode = 12;
                                        //    }                                                
                                        //}

                                        //if (statuscode > 0)
                                        //{
                                        //    statusdesc = repositorioAdm.CEstado(cabecera.IdTipoOrden,statuscode);
                                        //    int reslog = ALogAutorizacion(db,transaction,cabecera.CodigoCompania,//        cabecera.SalesOrg,cabecera.CodigoDivision,//        cabecera.IdTipoOrden,cabecera.TipoOrden,//        cabecera.NroPedido.ToString(),//        cabecera.CodigoEstado.ToString(),cabecera.Estado,//        statuscode.ToString(),statusdesc,"inter","");

                                        //    cabecera.CodigoEstado = statuscode;
                                        //    cabecera.Estado = statusdesc;
                                        //    cabecera.FechaEstado = Helpers.GetCurrentDateTime();

                                        //    savecabecera = true;

                                        //}
                                    }

                                    //Tabla de Error
                                    if (tablaEstatus.RowCount > 0)
                                    {
                                        //Obtengo el Estado
                                        statuscode = 9;
                                        cabecera.CodigoEstado = statuscode;
                                        statusdesc = repositorioAdm.CEstado(cabecera.IdTipoOrden,statuscode);
                                        cabecera.Estado = statusdesc;
                                        cabecera.FechaEstado = Helpers.GetCurrentDateTime();
                                        savecabecera = true;

                                        int reslog = ALogAutorizacion(db,cabecera.SalesOrg,cabecera.IdTipoOrden,cabecera.NroPedido.ToString(),cabecera.CodigoEstado.ToString(),statuscode.ToString(),"");
                                    }
                                }
                            }
                        }
                        if (savecabecera)
                        {
                            db.Configuration.ValidateOnSaveEnabled = false;
                            db.SaveChanges();
                        }

                        transaction.Complete();
                    }
                }


            }
        }
    }

ControlFacturasRemitosNotasCreditos内部的1220行:

status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();

ConnSAPNotasCreditos内部的第936行调用了ControlFacturasRemitosNotasCreditos函数:

ControlFacturasRemitosNotasCreditos(rfcDest,out nroerror);

有人可以告诉我这里的问题是什么吗?可能是SAP表格中的字段为空,如果我尝试将其复制到某个地方,那么整个崩溃了吗?在尝试复制该字段之前,是否有一种安全的方法来检查该字段是否为空?

解决方法

您所在行

status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();

您正在使用索引IRfcTable处理tableHstatus 0,这意味着您正在尝试从该表对象中读取第一个条目。仅在表具有条目的情况下才有效。您应该首先检查表是否包含任何行,然后再继续。调用RFC函数后,检查RowCount的属性IRfcTable

functionStatus.Invoke(rfcDest);
if (tableHstatus.RowCount > 0) {
    status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();
}

RFC函数可能返回空表的原因有很多。在某些情况下,这可能是标准行为,或者可能是因为某些查询参数错误并且找不到数据,或者由于函数发生错误。标准SAP BAPI通常返回状态结构或表(通常为BAPIRETURNBAPIRET2类型),其中包含有关错误代码,消息等的附加信息。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...