Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
888 views
in Technique[技术] by (71.8m points)

asp.net mvc - Crystal Report: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...