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

Crystal Report: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

我无法将值传递给报表。

这是我的代码:

public void GLRPT()
        {
            try
            {

                ReportClass rptH = new ReportClass();
                rptH.FileName = Server.MapPath("~/Rpts/G1.rpt");
                rptH.Load();

                string df = Session["fromdate"].ToString();
                string dt = Session["todate"].ToString();
                DateTime fromdate = DateTime.Parse(df);
                DateTime todate = DateTime.Parse(dt);

                rptH.SetParameterValue("?Date_From", fromdate);
                rptH.SetParameterValue("?Date_To", todate);
                rptH.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "GL");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

我不知道为什么会看到这个错误:
索引无效。 (HRESULT 异常:0x8002000B(DISP_E_BADINDEX))

我们应该像这样传递参数值:

rptH.SetParameterValue("Date_From", fromdate); //correct

rptH.SetParameterValue("?Date_From", fromdate); //incorrect

然后我们必须为报告提供数据库访问权限,因为如果不登录数据库,报告将无法打开。 这是代码:

ReportDocument rptH = new ReportDocument();
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;

rptH.Load(Server.MapPath("~/Rpts/G1.rpt"));

string df = Session["fromdate"].ToString();
string dt = Session["todate"].ToString();
DateTime fromdate = DateTime.Parse(df);
DateTime todate = DateTime.Parse(dt);

rptH.SetParameterValue("Date_From", fromdate);
rptH.SetParameterValue("Date_To", todate);

crConnectionInfo.ServerName = "YOUR SERVER NAME";
crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";
crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
crConnectionInfo.Password = "YOUR DATABASE PASSWORD";

CrTables = rptH.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
    crtableLogoninfo = CrTable.LogOnInfo;
    crtableLogoninfo.ConnectionInfo = crConnectionInfo;
    CrTable.ApplyLogOnInfo(crtableLogoninfo);
}

rptH.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "GL");

We must call the parameter's name without any additional character such as @ or ?, just only the parameter's name itself.

我知道这个话题有点老了,但如果我分享我的经验,可能会对某人有所帮助。

正如我们在许多主题中所读到的,"the parameters name are different in the report file and the code"。是的,这是真的,但如果您在报告文件中添加或删除参数而忘记将新文件复制到您的应用程序位置,也会发生这种情况。 这将是 Visual Studio 工作,但有时 VS 忘记将修改后的报告文件复制到 "Debug" 文件夹中,我想。

写这篇文章的重点,在你修改了你的报告文件之后,关于参数,将它复制到你的编译位置。