Crystal report shows database login when taking Dataset as datasource

3.8k views Asked by At

In my project i use EF4, but the connection string of the EF Context are dynamically generated and not saved in any Configuration file, everything works fine...

When i want to use Crystal report to view my reports, I used Datasets that import data form the my Entities (linq), and than set the Datasets as Datasources to the reports Here Is A Tutorial

But at the moment of the display (in the CrystalReportViewer) a login prompt window pops up, asking for Login & Password (also the Server name and database name but the fields are disabled)

Image

I Googled and i noticed that most of the solutions proposed concentrate around : Setting credentials in the code (like this) :

ConnectionInfo crconnectioninfo = new ConnectionInfo();
crconnectioninfo.ServerName = "localhost";
crconnectioninfo.DatabaseName = "dbclients";
crconnectioninfo.UserID = "ssssssss";
crconnectioninfo.Password = "xxxxxxx"; 

but here is how i create my report :

for (i = 0; i < 7; i++)
{
    DataRow dr = container.NewRow();
    dr = CreateRowFromMirrorRow(dr, i); //Methode that creates a Row
    container.Rows.Add(dr);
}

objRpt.SetDataSource(container.DefaultView);

// Binding the crystalReportViewer with our report object. 
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();
crystalReportViewer1.Visible = true;

My questions are :

  • Why do i have to pass any database credentials since there is no direct connection between the CrReport or the Dataset with the Database?

  • How do i set the credential of a Dataset (or crystal report) using a ConnectionInfo object (in my case)?

1

There are 1 answers

0
jjj On

In my experience, when you have Crystal Reports asking for credentials when it's not supposed to, there's something wrong that appears unrelated on the surface.

I think you might be able to get it to work if you don't call Refresh on the viewer, but call refresh on the report itself instead:

objRpt.SetDataSource(container.DefaultView);
objRpt.Refresh();

crystalReportViewer1.ReportSource = objRpt;
// Don't refresh viewer!
// If you have any report parameters, set them through the viewer here.

If for some reason, if you do need to set the connection for Crystal Report in the future, you can use this on the report before setting the report viewer's ReportSource:

foreach (Table crTable in report.Database.Tables)
{
    TableLogOnInfo tableLogOnInfo = crTable.LogOnInfo;
    var connectionInfo = tableLogOnInfo.ConnectionInfo;

    // use connectionInfo to set database credentials

    crTable.ApplyLogOnInfo(tableLogOnInfo);
}