尝试将值传递给crystal report文本框对象时发生异常

时间:2013-01-27 03:28:58

标签: c# crystal-reports

我正在尝试将值从C#代码传递到CrystalReport报表。

修改

private void PrintOrder(List<OrderPrintBO> pListOrderBO)
        {
            DSOrderReport oDSOrderReport = new DSOrderReport();
            DataTable oDataTable = oDSOrderReport.Tables[0];
            String sOrderNo = "";

            if (pListOrderBO.Count > 0)
            {
                for (int i = 0; i < pListOrderBO.Count; i++)
                {
                    DataRow oRow = oDataTable.NewRow();
                    oRow["OrderID"] = pListOrderBO[i].OrderID;
                    oRow["OrderNumber"] = pListOrderBO[i].OrderNumber;
                    sOrderNo = pListOrderBO[i].OrderNumber;
                    oDataTable.Rows.Add(oRow);
                }
            }

            oDSOrderReport.Merge(oDataTable);
            oDSOrderReport.AcceptChanges();

            if (oDSOrderReport.Tables[0].Rows.Count > 0)
            {
                // Main Copy
                PrintDialog printDialog = new PrintDialog();
                rptOrder oMainOrder = new rptOrder();

                String sCompanyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
                String sPhone1 = System.Configuration.ConfigurationManager.AppSettings["Phone1"].ToString();
                String sPhone2 = System.Configuration.ConfigurationManager.AppSettings["Phone2"].ToString();
                String sShowOrderNo = System.Configuration.ConfigurationManager.AppSettings["ShowOrderNo"].ToString();

                ((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtCompanyName"]).Text = sCompanyName;
                ((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtPhone1"]).Text = "Tel:" + sPhone1;
                ((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtPhone2"]).Text = "Tel:" + sPhone2;

////这个注释掉的部分给出了异常

 //string sVarOrderNo = "";

            //if (sShowOrderNo.ToLower() == "yes")
            //{
            //    sVarOrderNo = sOrderNo;
            //}


            //((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtOrderNo"]).Text = "O.N. : " + sVarOrderNo;

                oMainOrder.SetDataSource(oDSOrderReport);

                oMainOrder.PrintOptions.PrinterName = printDialog.PrinterSettings.PrinterName;


    try
                    {
                        oMainOrder.PrintToPrinter(1, false, 0, 0);
 MessageBox.Show("Order Printed Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("Error in Printing Order", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

结束修改

对于前三个文本框(CompanyName,Phone1和Phone2),每个工作正常,但当我尝试将OrderNo传递给报表时,索引超出了数组的范围。发生异常,即使我的报告有一个名为 txtOrderNo

的TextBox对象

我不确定为什么会这样。请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

尝试更改sOrderNo相关代码,如下所示。

string sVarOrderNo = "";

if (sShowOrderNo.ToLower() == "yes")
{
     sVarOrderNo =  sOrderNo;
}


((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtOrderNo"]).Text = "O.N. : " + sVarOrderNo;

<强>更新

您的问题是以下提到的代码段。在for loop旁边,您尝试为sOrderNo分配值。这是没有意义的。每次循环时它都会覆盖。那么这个目的是什么?如果您需要此值,则必须将此值作为表的行或作为报表的参数。

if (pListOrderBO.Count > 0)
            {
                for (int i = 0; i < pListOrderBO.Count; i++)
                {
                    DataRow oRow = oDataTable.NewRow();
                    oRow["OrderID"] = pListOrderBO[i].OrderID;
                    oRow["OrderNumber"] = pListOrderBO[i].OrderNumber;
                    sOrderNo = pListOrderBO[i].OrderNumber;
                    oDataTable.Rows.Add(oRow);
                }
            }

我希望这会对你有所帮助。