Crystal Report索引无效。 (HRESULT异常:0x8002000B(DISP_E_BADINDEX))

时间:2011-07-16 04:55:12

标签: asp.net crystal-reports-2008

我在asp.net上为web应用程序制作了一个水晶报告。有些报道开得很好。但有时却没有。它显示了以下问题。

  

索引无效。 (HRESULT异常:0x8002000B(DISP_E_BADINDEX))
  描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息   异常详细信息:System.Runtime.InteropServices.COMException:索引无效。 (HRESULT异常:0x8002000B(DISP_E_BADINDEX))
  来源错误:   在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息   堆栈跟踪:   [COMException(0x8002000b):索引无效。 (HRESULT异常:0x8002000B(DISP_E_BADINDEX))]
     Report_Viewer.Page_Load(Object sender,EventArgs e)+894
     System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e)+14
     System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e)+35
     System.Web.UI.Control.OnLoad(EventArgs e)+99
     System.Web.UI.Control.LoadRecursive()+50
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+627

版本信息:Microsoft .NET Framework版本:2.0.50727.4927; ASP.NET版本:2.0.50727.4927

我的VS:VS 2008,数据库:MS SQL Express 2005,操作系统:Windows 7

有没有人可以解决我的问题..

注意:我的代码在Class

public ReportDocument ShowReport(string ReportName, string Parameter1)
{                              
    try
    {
        cryRpt.Load(ReportName);            
        cryRpt.SetDatabaseLogon(UserName, Password, ServerName, DatabaseName);                                    
        crParameterValues.Clear();
        crParameterDiscreteValue.Value = Parameter1;// as like Parameter1
        crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
        crParameterFieldDefinition = crParameterFieldDefinitions["@Parameter1"];
        crParameterValues = crParameterFieldDefinition.CurrentValues;
        crParameterValues.Add(crParameterDiscreteValue);
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
    }
    catch (Exception ex)
    {
       throw ex;
       //ShowReport(@"default_report.rpt");
    }
    //CleareParameter();
    return cryRpt;
}

报告查看器页面加载

protected void Page_Load(object sender, EventArgs e)
{
    string ReportName="";
    string Parameter1="";
    ReportName= Session["ReportName "].ToString();
    Parameter1 = Session["Parameter1"].ToString();
    }
    try
    {
        if (Session["Parameter2"].ToString() != null)
        {
            Parameter2 = Session["Parameter2"].ToString();
            icount = icount + 1;
        }
    }
    catch (Exception ) { }
    ReportViewer oReportViewer = new ReportViewer(); // Class file
    ReportDocument rptDoc = new ReportDocument();
    try
    {
        rptDoc = oReportViewer.ShowReport(ReportName, Parameter1);
        rptDoc = oReportViewer.ShowReport(ReportName, Parameter1, 
        rptDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false, "Report");
        CrystalReportViewer1.ReportSource = rptDoc;
    }
    catch (Exception ex)
    {  
       throw ex;
    }       
}

5 个答案:

答案 0 :(得分:20)

这是因为您在代码中设置的参数(或参数名称)与报告中定义的参数不同

答案 1 :(得分:2)

  1. 添加一种方法
  2. Public Sub LoadReport()
        Try
            If Validation() = True Then Exit Sub
    
            If ClsDcon.ShowItems(Me.Text) = False Then Exit Sub
            Dim lvarr(4, 1) As String
    
            lvarr(0, 0) = "@CompId"
            lvarr(0, 1) = Connection.strGCompanyId  'strCompId
    
            lvarr(1, 0) = "@DepartmentId"
            lvarr(1, 1) = txtstaffAbscentees.GetTextBox.Tag      'strSDMUId
    
            lvarr(2, 0) = "@FromDate"
            lvarr(2, 1) = ClsDcon.ConvertDateToInt(dtpFromDate.GetTextBox.Text)                  'strFromDate
    
            lvarr(3, 0) = "@PFromDate"
            lvarr(3, 1) = "Staff Abscentees Report On: " & dtpFromDate.GetTextBox.Text 'ClsDcon.ConvertIntToDate(strFromDate)
    
            lvarr(4, 0) = "@Flags"
            lvarr(4, 1) = intFlag
    
            Me.Text = frmMain
            ClsDcon.LoadCrystalReport(New CrtStaffAbscentees, crpStaffAbscenteesReportviewer, lvarr)
        Catch ex As Exception
    
        End Try
    End Sub
    

    *的 2。添加加载和设置参数的功能 *

    Public Sub LoadCrystalReport(ByVal pvcrpReport As Object, ByVal pvcrvControl As CrystalReportViewer, Optional ByVal pvarrParamValues As Array = Nothing, Optional ByVal pvstrSelection As String = Nothing, Optional ByVal invVal As Integer = 0)
        Try
    
            Dim lvobjReport As Object = pvcrpReport
            'myRpt.Load("D:\Projects\MyAccountswin2008new\UILayer\Sales\Sales Reports\CrtPOSBillReport.rpt")
    
            For i As Integer = 0 To pvarrParamValues.GetUpperBound(0)
                lvobjReport.SetParameterValue(pvarrParamValues(i, 0).ToString, pvarrParamValues(i, 1))
            Next
    
            Dim lsMethod As String = System.Reflection.MethodBase.GetCurrentMethod().Name
            Dim loConnInfo As New CrystalDecisions.Shared.ConnectionInfo
            'Dim lsMode As String = Configuration.ConfigurationSettings.AppSettings("Mode")
            loConnInfo.ServerName = My.Settings.RPTServerName
            loConnInfo.DatabaseName = My.Settings.RPTDatabaseName
            loConnInfo.UserID = My.Settings.RPTUserID
            loConnInfo.Password = My.Settings.RPTPassword
            Dim loTables As CrystalDecisions.CrystalReports.Engine.Tables
            Dim loTable As CrystalDecisions.CrystalReports.Engine.Table
            Dim loTableLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
            loTables = lvobjReport.Database.Tables
            For Each loTable In loTables
                loTableLogonInfo = loTable.LogOnInfo
                loTableLogonInfo.ConnectionInfo = loConnInfo
                loTable.ApplyLogOnInfo(loTableLogonInfo)
                loTable.Location = loTable.Name
            Next
            'pvcrvControl.RefreshReport()
            pvcrvControl.ReportSource = lvobjReport
            pvcrvControl.Refresh()
            'pvcrvControl.RefreshReport()
            If invVal = 1 Then
                Dim prd As New System.Drawing.Printing.PrintDocument
                lvobjReport.PrintOptions.PrinterName = prd.PrinterSettings.PrinterName
                If lvobjReport.PrintOptions.PrinterName = "" Or lvobjReport.PrintOptions.PrinterName = Nothing Then
                    MyMessagebox.MyMessagebox.ShowMessage("Set Default Printer", "Global Funtions_UI", MessageBoxIcon.Information)
                End If
    
                'prd.DefaultPageSettings.PaperSize.RawKind = PaperKind.Custom
                'prd.DefaultPageSettings.PaperSize.PaperName = "NewSize"
                'Dim prnPS As New Printing.PaperSize("HalfFolio", 850, 650)
                'Dim intIdx As Integer
    
                'For intIdx = 0 To prd.PrinterSettings.PaperSizes.Count - 1 Step 1
                '    If prd.PrinterSettings.PaperSizes(intIdx).Kind = Printing.PaperKind.Custom Then
                '        prnPS.RawKind = prd.PrinterSettings.PaperSizes(intIdx).RawKind
                '        prd.DefaultPageSettings.PaperSize = prnPS
                '    End If
                'Next
                'prd.Print()
    
    
    
    
                'If prd.DefaultPageSettings.PaperSize.Kind = PaperKind.Custom Then
                '    prd.DefaultPageSettings.PaperSize.Width = 827
                '    prd.DefaultPageSettings.PaperSize.Height = 650
                NumberofPrints()
                lvobjReport.PrintToPrinter(dblPrintValue, False, 0, 0)
                'End If
            ElseIf invVal = 2 Then
                Dim prd As New System.Drawing.Printing.PrintDocument
                lvobjReport.PrintOptions.PrinterName = prd.PrinterSettings.PrinterName
                If lvobjReport.PrintOptions.PrinterName = "" Or lvobjReport.PrintOptions.PrinterName = Nothing Then
                    MyMessagebox.MyMessagebox.ShowMessage("Set Default Printer", "Global Funtions_UI", MessageBoxIcon.Information)
                End If
    
                NumberOfReturnPrints()
                lvobjReport.PrintToPrinter(dblReturnPrintValue, False, 0, 0)
    
            End If
        Catch ex As Exception
            MYERROR.WriteLogFile("GlobalFunctions_UI", "LoadCrystalReport", ex)
        End Try
    
    End Sub
    

答案 2 :(得分:1)

当您从上一页更改页面大小设置,并且忘记更改代码以强制使用上一页的设置进行打印时,也会发生此错误。

例如A5> A4,但是使用代码您仍将页面大小设置为A5。

答案 3 :(得分:1)

crParameterFieldDefinitions [“ @ Parameter1”];

在上一行中,您将检查参数是否存在。我认为那不可用。

希望这是有帮助的!

答案 4 :(得分:0)

当两个报表和编码中的参数都不匹配时,会发生此错误。可能是您的参数名称错误。因此,请验证报表和您的编码中的参数和参数名称是否相同。