Origin of the problem:
Company for the new system, the transition period to coexist with the old system, the original report using an ODBC data source, because there is only one server, all new system reports can no longer use ODBC data source, (two system reports cannot use the same server ODBC data source)
Solution: Plan One, modify the data source for the new system Crystal Report, and change to OLE DB mode.
Scenario Two, modify the Crystal Report data source to push mode, dynamically set the data source in the program.
Some reports use scenario one, which is suitable for reports with large data volumes and multiple pages. (Financial statements)
Because scenario one needs to modify the data source for each report, the setup is too cumbersome, and the company has too many report files, nearly hundreds of reports per system. There are 5, 6 systems, and scheme one is too inflexible. So for a small amount of data usage scenario two.
After a few days looking for data to test, finally run through.
There are probably two ways: The Reportdocument object, the report data source control.
The report data source control works for previews, and Reportdocument objects are more flexible, but why can't I set up a data source dynamically? An instance of object reference has not been set to objects.
The report data source control crystalreportsource1.reportdocument An occasional inexplicable error when sending messages as a parameter .
Following:
Reportdocument rd = new Reportdocument ()
Case "Manifest":
Load a report file
Rd. FileName = MapPath ("Manifest_a4.rpt");
CrystalReportSource1.ReportDocument.Load (Server.MapPath ("manifest_a4.rpt"));
First step: Set up a data source
DataSet ds = Getrddatamainfest (SiteID, kid);
Setdatasource (ds, RD);
Setdatasource (ds, crystalreportsource1.reportdocument);
The second step: set the parameters, this must be set after the data source, or there is a missing parameter value error.
Rd. Setparametervalue ("show", isshow);
CrystalReportSource1.ReportDocument.SetParameterValue ("show", isshow);
ReportName = "Manifest manifest#" + li[0];
Part III: Selecting formulas
Strrptdbkey = "{View_manifest_all. Manifest_num} ";
Strrptdbsite = "{View_manifest_all. SiteID} ";
Rd. Recordselectionformula = sb. ToString ();
CrystalReportSource1.ReportDocument.RecordSelectionFormula = sb. ToString ();
Fourth step: Bind the control
Crystalreportviewer1.reportsource = CrystalReportSource1;
Crystalreportviewer1.reportsource = rd;//The object reference is not set to an instance of the object.
Crystalreportviewer1.databind ();
Me.showpdfreport (crystalreportsource1.reportdocument);
Me.showpdfreport (RD);
Public DataSet getrddatamainfest (string SiteID, String kid)
{
The
//sql statement laughed at the
string strSQL = "SELECT * from ATP_MASTER.dbo.site where siteid= '" + SiteID + "';";
strSQL + = "SELECT * from view_mawb_hawb_coload_rpt where siteid= '" + SiteID + "' and manifest_num= '" + Kid + "';" ;
strSQL + = "SELECT * from View_mawb_all where siteid= '" + SiteID + "' and manifest_num= '" + Kid + "';";
strSQL + = "SELECT * from view_mawb_hawb_rpt where siteid= '" + SiteID + "' and manifest_num= '" + Kid + "';";
strSQL + = "SELECT * from Mawb_loading_info where siteid= '" + SiteID + "' and manifest_num= '" + Kid + "';";
Response.Write (strSQL);
String connstr = Logistic.Common.Common.GetAeConnectionString ();
DataSet ds = Logistic.SqlConn.SqlHelper.ExecuteDataset (ConnStr, CommandType.Text, strSQL);
DS. Tables[0]. TableName = "SITE";
DS. TABLES[1]. TableName = "View_man_coload_rpt";
DS. TABLES[2]. TableName = "View_manifest_all";
DS. TABLES[3]. TableName = "VIEW_MANIFEST_HAWB";
DS. TABLES[4]. TableName = "Manifest_loading_info";
Be sure to set the table name
return DS;
}
Append a data source to the main report and subreport.
public void Setdatasource (DataSet ds, Reportdocument Rd)
{
foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in Rd. Database.tables)
{
Tbl. Setdatasource (ds. Tables[tbl. Name]);
}
foreach (reportdocument subrd in Rd. subreports)
{
foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in SubRd.Database.Tables)
{
Tbl. Setdatasource (ds. Tables[tbl. Name]);
}
}
}
Add, for the subreport parameter added, later will be used to update.
Crystal Report Push mode