views:

1029

answers:

1

Hello,

I have been struggling with that for 4 days now. I have a very very simple crystal report(I am using it just for a proof of concept). The report is bound to a database and I display only one field from one table in the database.No subreports.It was created with Crystal Reports 2008. I need to display this report in my .Net MVC web app but I need to be able to change the database connection information since this app. will be used against different databases with identical table structure. So I created a standard web form and I dragged a CrystalReportViewer and CrystalReportSource to it.

This is my code:

protected void Page_Load(object sender, EventArgs e)
    {
        this.CrystalReportSource1.EnableCaching = false;
        this.CrystalReportSource1.ReportDocument.Load(@"C:\ReportName.rpt");

        //1)  I get the data connection variables from my app - this part works well  
              and is irrelevant in this case.

 //2) Once I have the data I need to apply it to the connection of the report
 ConnectionInfo crConnection = new ConnectionInfo();
 crConnection.UserID = userID;
 crConnection.ServerName = datasource;
 crConnection.DatabaseName = "";
 crConnection.Password = password;


 AssignConnectionInfo(CrystalReportSource1.ReportDocument,crConnection);

 CrystalReportSource1.ReportDocument.DataSourceConnections[0].SetConnection 
 (crConnection.ServerName, crConnection.DatabaseName, false);

CrystalReportSource1.ReportDocument.SetDatabaseLogon(crConnection.UserID, 
crConnection.Password, crConnection.ServerName, crConnection.DatabaseName);


CrystalReportViewer1.ReportSource = CrystalReportSource1.ReportDocument;
CrystalReportViewer1.RefreshReport();

 }//close the page load function

This is the AssignConnectionInfo Function:

private void AssignConnectionInfo(ReportDocument document,ConnectionInfo crConnection)
    {
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in document.Database.Tables)
        {
            TableLogOnInfo logOnInfo = table.LogOnInfo;
            if (logOnInfo != null)
            {
                table.ApplyLogOnInfo(table.LogOnInfo);
                table.LogOnInfo.TableName = table.Name;
                table.LogOnInfo.ConnectionInfo.UserID = crConnection.UserID;
                table.LogOnInfo.ConnectionInfo.Password = crConnection.Password;
                table.LogOnInfo.ConnectionInfo.DatabaseName = crConnection.DatabaseName;
                table.LogOnInfo.ConnectionInfo.ServerName = crConnection.ServerName;

                CrystalReportViewer1.LogOnInfo.Add(table.LogOnInfo);

            }
        }



    }

so what's happening is that the page loads and the Crystal banner, toolbar displays but the databound field that I have in my report is blank. Do u see anything wrong?

Thanks very very much in advance

Susan

A: 

Instead of loading a report source object. I think the only thing you need to do is create a ReportDocument object, load the report, set the connection info using the function that you have, and pass the viewer the report. You should be able to leave out the ReportSource part like this:

protected void Page_Load(object sender, EventArgs e)
{
    ReportDocument doc = new ReportDocument();
    doc.Load(@"C:\ReportName.rpt");

    ConnectionInfo crConnection = new ConnectionInfo();
    crConnection.UserID = userID;
    crConnection.ServerName = datasource;
    crConnection.DatabaseName = "";
    crConnection.Password = password;

    AssignConnectionInfo(doc,crConnection);

    CrystalReportViewer1.ReportSource = doc;

 }//close the page load function

The AssignConnectionInfo function does the work of a lot of the other code that you had in there. Also, you shouldn't need the the following code:

CrystalReportViewer1.RefreshReport();

The report should run when the page loads so this will just cause the report to run a second time. Hope this helps.

Dusty
Thanks very much. Actually what made a difference is transferring the code from the Load function to the Init function of the Reportviewer object. It works now. Thanks again for all the help.
suzi167