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

C# SAP.Middleware.Connector.RfcInvalidParameterException:表行0超出范围:表为空,c#,exception,sap,console-application,saprfc,C#,Exception,Sap,Console Application,Saprfc,我有一个C#控制台应用程序,可以连接到SAP服务器,从其中的一些表中获取数据。 有时,当我试图将SAP表中名为“SOHEADER”的字段中的值复制到字符串变量时,会弹出此错误。 以下是堆栈跟踪: SAP.Middleware.Connector.RfcInvalidParameterException:表行0 超出范围:表为空 SAP.Middleware.Connector.RfcTable.get_项(Int32 lineIndex)en ExportarPedidosSAP.Program

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

SAP.Middleware.Connector.RfcInvalidParameterException:表行0 超出范围:表为空 SAP.Middleware.Connector.RfcTable.get_项(Int32 lineIndex)en ExportarPedidosSAP.Program.controlfacturesremitosnotascreditos(RFCD估算 rfcDest、字符串和nroerror)en C:\Users\USER\Source\repos\BSCPY\BSCPY\ExportarPedidosSAP\Program.cs:línea 1220 en ExportarPedidosSAP.Program.ConnSAPNotasCreditos()en C:\Users\USER\Source\repos\BSCPY\BSCPY\ExportarPedidosSAP\Program.cs:línea 936

以下是ControlFacturesRemitosNotasCreditos功能:

    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, transaction, cabecera.CodigoCompania,
                                            cabecera.SalesOrg, cabecera.CodigoDivision,
                                            cabecera.IdTipoOrden, cabecera.TipoOrden,
                                            cabecera.NroPedido.ToString(),
                                            cabecera.CodigoEstado.ToString(),
                                            cabecera.Estado,
                                            statuscode.ToString(),
                                            statusdesc, "inter", "");
                                    }
                                }
                            }
                        }
                        if (savecabecera)
                        {
                            db.Configuration.ValidateOnSaveEnabled = false;
                            db.SaveChanges();
                        }

                        transaction.Complete();
                    }
                }


            }
        }
    }
ControlFacturasRemitosNotasCreditos(rfcDest, out nroerror);
ConnSAPNotasCreditos中调用ControlFacturesRemitosNotasCreditos函数的第936行:

    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, transaction, cabecera.CodigoCompania,
                                            cabecera.SalesOrg, cabecera.CodigoDivision,
                                            cabecera.IdTipoOrden, cabecera.TipoOrden,
                                            cabecera.NroPedido.ToString(),
                                            cabecera.CodigoEstado.ToString(),
                                            cabecera.Estado,
                                            statuscode.ToString(),
                                            statusdesc, "inter", "");
                                    }
                                }
                            }
                        }
                        if (savecabecera)
                        {
                            db.Configuration.ValidateOnSaveEnabled = false;
                            db.SaveChanges();
                        }

                        transaction.Complete();
                    }
                }


            }
        }
    }
ControlFacturasRemitosNotasCreditos(rfcDest, out nroerror);
有人能告诉我这里有什么问题吗?是否SAP表中的字段是空的,如果我试图将其复制到某个地方,整个过程就会崩溃?在尝试复制字段之前,是否有安全的方法检查字段是否为空?

在您的行中

status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();
您正在使用索引
0
处理
IRfcTable
tableHstatus
,这意味着您正在尝试从该表对象读取第一个条目。这仅在表中有条目时有效。您应该首先检查表是否包含任何行,然后再继续。调用RFC函数后,检查
IRfcTable
的属性
RowCount

functionStatus.Invoke(rfcDest);
if (tableHstatus.RowCount > 0) {
    status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();
}
RFC函数返回空表的原因有很多。在某些情况下,这可能是标准行为,也可能是因为某些查询参数错误且未找到数据,或者是因为函数中出现错误。标准SAP BAPI通常返回一个状态结构或表(通常为
BAPIRETURN
BAPIRET2
),其中包含有关错误代码、消息等的附加信息