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

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


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

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)