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

1

I can not pass values to the report.

This is my code:

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);
            }
        }

I don't know why I see this error:
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

asp.net-mvc
database
crystal-reports
asked on Stack Overflow Mar 8, 2016 by (unknown user) • edited Mar 9, 2016 by (unknown user)

2 Answers

2

We should pass the parameter value like this:

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

NOT

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

Then we we must give database access to the report because without login to database the report will not be opened. and here is the code:

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.

answered on Stack Overflow Mar 12, 2016 by (unknown user) • edited Mar 12, 2016 by (unknown user)
0

I know the topic a little bit old, but it might help for someone if I share my experience.

As we can read in many topics, "the parameters name are different in the report file and the code". Yes this is true, but it is also happens if you added or removed parameters from the report file and forgot to copy the new file into your application location. It would be the Visual Studio job, but sometimes the VS forget to copy the modified report file to the "Debug" folder, I think.

The point of this writing, after you modified your report file, regarding parameters, copy it into your compiled location.

answered on Stack Overflow Oct 19, 2018 by Zoltan

User contributions licensed under CC BY-SA 3.0