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
725 views
in Technique[技术] by (71.8m points)

c# - database login prompt with crystal reports

I'm trying to display some report with some subreports inside it, but every it shows the report it throws some dialog box asking for database connection. I'm using this code:

private void frmReporte_Load(object sender, System.EventArgs e)
    {
        Clave = ConfigurationSettings.AppSettings["Password"].ToString();
        NombreBD = ConfigurationSettings.AppSettings["CatalogBD"].ToString();
        NombreServidor = ConfigurationSettings.AppSettings["Servidor"].ToString(); ;
        UsuarioBD = ConfigurationSettings.AppSettings["UserID"].ToString();
        this.crtReportes.ReportSource = this.prepareReport();
    }
    public void imprimirReporte()
    {
        ReportDocument rpt = new ReportDocument();
        rpt.Load(mvarRutaReporte);
        rpt.SetDataSource(clsReportes.dsReporte);
        rpt.PrintToPrinter(1, false, 1, 1);
    }
    private ReportDocument prepareReport()
    {
        Sections crSections;
        ReportDocument crReportDocument, crSubreportDocument;
        SubreportObject crSubreportObject;
        ReportObjects crReportObjects;
        ConnectionInfo crConnectionInfo;
        Database crDatabase;
        Tables crTables;
        TableLogOnInfo crTableLogOnInfo;
        crReportDocument = new ReportDocument();
        crReportDocument.Load(RutaReporte);
        crReportDocument.SetDataSource(clsReportes.dsReporte.Tables[0]);
        crDatabase = crReportDocument.Database;
        crTables = crDatabase.Tables;
        crConnectionInfo = new ConnectionInfo();
        crConnectionInfo.ServerName = NombreServidor ;
        crConnectionInfo.DatabaseName = NombreBD;
        crConnectionInfo.UserID = UsuarioBD;
        crConnectionInfo.Password = Clave;
        foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
        {
            crTableLogOnInfo = aTable.LogOnInfo;
            crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
            aTable.ApplyLogOnInfo(crTableLogOnInfo);
        }
        // Para los reportes que poseen subreportes 
        // pongo el objeto seccion del la seccion actual del reporte 
        crSections = crReportDocument.ReportDefinition.Sections;
        // busco en todas las secciones el objeto reporte
        foreach (Section crSection in crSections)
        {
            crReportObjects = crSection.ReportObjects;
            //busco en todos los reportes por subreportes
            foreach (ReportObject crReportObject in crReportObjects)
            {
                if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                {
                    crSubreportObject = (SubreportObject)crReportObject;
                    //abro el subreporte y me logeo con los datos del reporte general
                    crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
                    crDatabase = crSubreportDocument.Database;
                    crTables = crDatabase.Tables;
                    foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
                    {
                        crTableLogOnInfo = aTable.LogOnInfo;
                        crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
                        aTable.ApplyLogOnInfo(crTableLogOnInfo);
                    }
                }
            }
        }
        return crReportDocument;
    }
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I had a similar problem and it is solved now, so I'm adding this reply in case it might help someone else in my situation.

When setting the SQL Server login info for the report, make sure you include the service name. So, for example, make sure you're giving Crystal "myservermyservice" instead of just "myserver".

My program is able to access data from SQL Server using just "myserver", but Crystal needs to be given "myservermyservice".


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

...