Crystal reports, why is it asking for database login even after I provided the details?

25,635

Solution 1

We have lots of problems when we came to connect a crystal report to a different database to the one it was designed / created against. We ended up creating the following function to set it up correctly. It recursively sets the connection on all report tables and sub reports.

Also I seem to remember we had problems if the report was designed with Integrated Windows Authentications, and run with a simple username and password. So we always made sure we designed the reports with a simple username and password connection to the database.

private static void SetConnection(ReportDocument report, string databaseName, string serverName, string userName, string password)
{
    foreach (Table table in report.Database.Tables)
    {
        if (table.Name != "Command")
        {
            SetTableConnectionInfo(table, databaseName, serverName, userName, password);
        }
    }

    foreach (ReportObject obj in report.ReportDefinition.ReportObjects)
    {
        if (obj.Kind != ReportObjectKind.SubreportObject)
        {
            return;
        }

        var subReport = (SubreportObject)obj;
        var subReportDocument = report.OpenSubreport(subReport.SubreportName);
        SetConnection(subReportDocument, databaseName, serverName, userName,  password);
    }
}

private static void SetTableConnectionInfo(Table table, string databaseName, string serverName, string userName, string password)
{
    // Get the ConnectionInfo Object.
    var logOnInfo = table.LogOnInfo;
    var connectionInfo = logOnInfo.ConnectionInfo;

    // Set the Connection parameters.
    connectionInfo.DatabaseName = databaseName;
    connectionInfo.ServerName = serverName;
    connectionInfo.Password = password;
    connectionInfo.UserID = userName;
    table.ApplyLogOnInfo(logOnInfo);

    if (!table.TestConnectivity())
    {
        throw new ApplicationException(Resource.UnableToConnectCrystalReportToDatabase);
    }

    table.Location = Database + "." + "dbo" + "." + table.Location;
}

Solution 2

It only appears when your DataSet or DataTable is empty. So make sure it has data.

Solution 3

Just create report object and add below link rptobj is crystalreport object and setdatabaselogin is method which take user id and password as a parameter.

rptobj.setdatabaselogon(userid,password)

Share:
25,635
Razort4x
Author by

Razort4x

Updated on July 23, 2022

Comments

  • Razort4x
    Razort4x almost 2 years

    I am generating a report, but the problem is even though I've supplied credentials, when the form containing the CrystalReport opens up, it still asks me for them, and the worst part is, I don't enter any thing in there, and just click finish, and it loads the report. So, if there is no need for credentials (or whatever) why is it asking me?

    Here's the code

        private void MainReport_Load(object sender, EventArgs e)
        {
            var constr = string.Empty;
            constr = Application.StartupPath;
            if (Generate.bForProjects)
                constr = Path.Combine(constr, @"Reports\Projects.rpt");
            else
                constr = Path.Combine(constr, @"Reports\Other.rpt");
    
            var myConInfo = new CrystalDecisions.Shared.TableLogOnInfo();
            reportDocument1.Load(constr);
            myConInfo.ConnectionInfo.DatabaseName = "ProjectData.mdb";
            myConInfo.ConnectionInfo.ServerName = Application.StartupPath + @"\Data\ProjectData.mdb";
            myConInfo.ConnectionInfo.Password = "";
            myConInfo.ConnectionInfo.UserID = "";
            reportDocument1.Database.Tables[0].ApplyLogOnInfo(myConInfo);
    
            reportDocument1.Refresh();
    
            crystalReportViewer1.ReportSource = reportDocument1;
            crystalReportViewer1.Width = this.Width - 50;
            crystalReportViewer1.Height = this.Height - 100;
        }
    

    When the form loads, this screen pop ups

    enter image description here

    And, when this comes up, I don't enter anything! That's right! I just click finish and it loads the report perfectly! So, if it doesn't needs anything, why the hel* is it asking me for a login?

  • Maximiliano Rios
    Maximiliano Rios over 10 years
    I tried to use this methods you created and they don't work. It continues asking me for the old database, it never changes. Do you have any suggestion?
  • Sushil Jadhav
    Sushil Jadhav over 5 years
    From "Database Expert" option, I have removed all the tables from dataset which are not being used in report. This works for me.