Method One: the first generation expert reports Crystal Reports, and then loaded: using CrystalDecisions.Shared; // TableLogOnInfo responsible for interpreting the class using CrystalDecisions.CrystalReports.Engine; // responsible for interpreting the ReportDocument class private void Page_Load (object sender, System.EventArgs e) {// Place the user code here to initialize the page try {crystalReportViewer1.Database ();} catch (exception me) {response.write (me.tostring ());}
} Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is necessary for the ASP.NET Web Form Designer. // InitializationComponent ();
// InitializeComponent, try {// Binds through the report object (viable because you can specify logoninfo) TablelogonInfo LogonInfo = New TableLogonInfo (); ReportDocument1 = New ReportDocument ();
reportDocument1.Load (@ "c: /inetpub/wwwroot/RS_Web_test/CrystalReport1.rpt"); // @ "C: / Program Files / Microsoft Visual Studio .NET 2003 / Crystal Reports / Samples / Reports / General Business / CrystalReport1. RPT "); // Password-protected Microsoft Access and Paradox and other PC databases also use this method // but logoninfo.servername and logoninfo.databaseename want to keep logoninfo.connectioninfo.servername =" "; logoninfo.connectioninfo.databaseName = ""; logOnInfo.ConnectionInfo.UserID = "sa"; logOnInfo.ConnectionInfo.Password = "sa"; reportDocument1.Database.Tables [0] .ApplyLogOnInfo (logOnInfo); CrystalReportViewer1.ReportSource = reportDocument1;} catch (Exception ee) { Response.write (EE.TOSTRING ());
Base.oninit (E);} Method 2: It is recommended to add the code all in Page_Load. Moreover, the code one's code has two disadvantages: (1) Data report format file is a ServerName, Database, UserId, Password, which uses absolute path (2) database access, is written, and the extensibility is not good. Advanced Improvement: (1) Server.mappath function (path to C: // inetpub // wwwroot) (2) Read Web.config (System.configuration.configurationSettings.AppSettings ["ServerName"];) Code: Using CrystalDecisions.Shared; // TableLogOnInfo responsible for interpreting class using CrystalDecisions.CrystalReports .Engine; // class is responsible for interpreting ReportDocument private void Page_Load (object sender, System.EventArgs e) {TableLogOnInfo logOnInfo = new TableLogOnInfo (); // here must first declare A ReportDocument object Report, simultaneously load data report reportDocument orpt = new reportDocument (); // Get the .rpt file true path string path1; Path1 = Server.mAppath ('' // rs-proj ''); Path1 = '' / /cr1.rpt' ';/orpt.load("c://inetpub//wwwroot//rs-proj//cr1.rpt" ";orpt.load (pat1);
// Get the parameter information from the web.config logOnInfo string a, b, c, d; // Get ServerNamea = System.Configuration .ConfigurationSettings .AppSettings [ "servername"]; // Get DatabaseNameb = System.Configuration .ConfigurationSettings .AppSettings [ "database"]; // Get UserIdc = System.Configuration .ConfigurationSettings .AppSettings [ "userid"]; // Get passwordd = System.Configuration .ConfigurationSettings .AppSettings [ "pass"]; // set the parameters logOnInfo.ConnectionInfo logOnInfo .Servername = a; logoninfo.connectioninfo.databaseName = B; logoninfo.connectioninfo.userid = C; logoninfo.connectioninfo.password = D;
ORPT.DATABASE .TABLES [0] .applylogoninfo (logonfo);
// Establish the connection between the.RPT file and the CrystalReportViewer file. CrystalReportViewer1.reportsource = ORPT;}