XSD Disconnected Crystal Report DataSource Part 2

XSD Disconnected data sets can be quite tricky when calling from a web application.  In this final post I will show how to call a Crystal Report from a web server using a disconnected XSD datasource.

XSD Code

C#
using System.IO;
...
//Create connection and generate sample dataset
...
//Open your connection
System.Data.SqlClient.SqlConnection tempCN = new System.Data.SqlClient.SqlConnection();
...
//Open your dataset
System.Data.DataSet tempDS = new System.Data.DataSet();
...

//I like to append 'DS' and 'DT' to identify in the XSD files and Crystal
tempDS.DataSetName = txtPageAppName.Text.Trim() + "DS";
tempTB = tempDS.Tables[0];
tempTB.TableName = txtPageAppName.Text.Trim() + "DT";

//Set report variables
CrystalDecisions.CrystalReports.Engine.ReportDocument rep = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
rep.Load(Server.MapPath(txtReportPath.Text.Trim()));
rep.SetDataSource(tempTB);
rep.Refresh();
rep.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, txtPageAppName.Text.Trim() + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Day.ToString() + "-" + System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.TimeOfDay.ToString());

Follow-up

In my sample code I used several field names to enter in the name of the ApplicationName, ReportPath, etc.  These names can be hard-coded or stored in a database.  For consistency and reliability the DataSetName and TableName values have to be the same as when you originally created the Crystal Report from the XSD in part 1.

Set Formula Fields

As with most reports, there is a report name, report description, domain name, execution date, etc that must be displayed on the report.  To do this you simply add a ‘Formula Field’ on the report and access it via code like this:

C#
//Report Description
            try
            {
                pobjCrystalReport.DataDefinition.FormulaFields["txtReportDescription1"].Text = "'" + strReportDescription + "'";
            }
            catch (System.Exception)
            {
            }

            //Report Domain
            try
            {
                pobjCrystalReport.DataDefinition.FormulaFields["txtDomain1"].Text = "'" + System.Environment.UserDomainName.ToString() + "'";
            }
            catch (System.Exception)
            {
            }

            //Report Execution Date
            try
            {
                pobjCrystalReport.DataDefinition.FormulaFields["txtDate1"].Text = "'" + System.DateTime.Now.ToShortDateString() + " at " + System.DateTime.Now.ToShortTimeString() + "'";
            }
            catch (System.Exception)
            {
            }
Scroll to Top