简体   繁体   中英

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

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 "myserver\myservice" instead of just "myserver".

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

In crystal you can specify whether to prompt for db login @ the time of report generation in Crystal Management Console (CMC) @ each report level. Login to CMC, open your report > Process > Database. At the bottom of the page you can specify "Prompt the user for database logon", "Use SSO context for database logon" or "Use same database logon as when report is run". Select the third option for using the credentials stored in the server.

You can just write report.SetDatabaseLogon("username", "pwd", @"server", "database"); give the login information then the database logon box will not appear when you will run the code.

Some older Crystal Report files saved with prior versions of the SQL Native Client (eg Provider: SQLNCLI) will no longer be able to login if either the client or server disables the respective TLS protocol version. For example, if a report was saved with SQL Server 2005 (version 9.00.xx), then TLS 1.0 is needed. If this protocol is disabled, there's no intuitive error, simply a "Database logon failed" is seen programmatically. Client software including rendering in a web browser will be prompted to enter a password, even though the account may have proper SQL permissions.

If rendering reports in IIS applications, see a tool called IISCrypto from Nartac Software to easily turn on and off various protocols, ciphers, hashes, and key exchanges.

Manually editing the report file and using Database - Set Datasource Location when connecting with a more modern native client is one way to fix this so that obsolete and insecure protocols don't need to remain enabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM