Everyone uses a crystal statement based on WebForm, if it is simple to follow online "A knife", it will definitely prompt you: landing failed. For this problem, I spent a whole day to study the help document of the crystal report, and finally get the solution. I am not a conservative person, I believe there are many netizens who are spending with me in studying this problem. I don't want everyone to spend energy as me as me. Below is a few steps I implemented. Don't worry, slowly look at you will have a lot of help. Step 1: Look at the sample file Crystal Report comes with an example file, the database is Access (without password). I first run the sample file (webform and winform) results show the correct report, as examples of "flying knives". So I built a report file and .aspx file, the result shows that the landing failed! But I convert the report file to an example report file, no longer an error. Where is the problem? Is it a report format file about login privilege? By tracking, debugging, I compare my report files and sample report files without discovering any differences. It seems that the problem is not
In the report file. Is the problem of a database? I created an Access result or landable to fail! Not a database problem (my own database is unparalleled access, the database in the help)? Nor is the problem of report format file (I carefully analyze the original code of the two)? So where is the problem? I can't understand! Step 2: Find a help file, then I will call again to help. I have rushed over the help of the crystal report, I finally found "Access Secure Database [C #]" words, find that there is a word below: Access the process of securing the secure database through the Crystal Reports for Visual Studio .NET in Web Forms and Windows Forms There is different. In the Windows Form, the dialog is automatically
Prompt users enter the username and password (the test is easy to succeed). In a web form, you need to design a form to get this information from the user. For both cases, you can use code to specify user names and
Passwords provide the same security level for all users of the application.
So I am the same database (first use the Access database in the help example, then use the Access database created by yourself). Found for the same report file, for WinForm can show success,
Webform still shows the landing failed! So I a little understanding! It seems that the problem is on the setting of permissions.
Step 3: Research help, finally succeeded! In the help I found the words "Setting Database Login Parameters", provided some information that later discovered is very useful: The following example shows how to pass the login parameters to the table of the report. This example uses a connection to a secure SQL Server database.
Start a new project Add a "button" to the form and four "text box" controls. Name the "text box" control, respectively: ServerNametXT, DBNametXT, UserNametxt, and Passwordtxt. Double-click the "Button" control to specify the code of the Click event. Insert the appropriate code according to the language used. [C #] // Declare the required variables. TablelogonInfo LogonInfo = New TablelogonInfo (); int i = 0;
// loop in turn on each table in the report. For (i = 0; i == report.database.tables.count - 1; i ) {// set the connection information of the current table. logOnInfo.ConnectionInfo.ServerName = serverNameTxt.Text; logOnInfo.ConnectionInfo.DatabaseName = dbNameTxt.Text; logOnInfo.ConnectionInfo.UserID = userNameTxt.Text; logOnInfo.ConnectionInfo.Password = passwordTxt.Text; Report.Database.Tables [i] .ApplyLogOnInfo (LogonInfo); Note that the password-protected Microsoft Access and PARADOX and other PC databases also use this method, but logoninfo.servername and Logoninfo.databaseName are left empty. So I like this, try it, an error message: No TablelogonInfo and Report were found. Later I found that TablelogonInfo is a member belonging to the CrystalDecisions.Shared namespace. So I added a reference: using crystaldecisions.shared; this issue appears in Report. REPORT? What is something, I have all the help, there is no such function or class! [honestly! This problem is hard to have a long time! I have been looking for Report to something! The water view company is really, help, don't write more than a matter! This is! ! ! ] Eventually I finally found that Report is just a user-defined object, not an object of the system itself. When I was confused, suddenly I thought, why not look at the Database behind the Report, what is something, I finally solved it, find the following information reportDocument orpt = new reportDocument (); Report belongs to DocumentCrystaldecisions.crystalReports .Engines. A member of the class. Modifying code: and add reference using crystaldecisions.shared; // Responsible to explain the TableLogonInfo class using crystaldecisions.crystalReports .Engine; // Responsible to explain the ReportDocument Class Private Void Page_load (Object Sender,
System.EventArgs e) {TableLogOnInfo logOnInfo = new TableLogOnInfo (); // declare a ReportDocument where objects must first Report, while loading data report ReportDocument oRpt = new ReportDocument (); oRpt.Load ( "c: // inetpub // wwwroot //exer/pagelet//crystal//cr1.rpt" ";/ modified to your self //'s correct position
// Establish a secure information // Password protected Microsoft Access and Parad database, etc., but logoninfo.servername // and logoninfo.databaseename want to keep logoninfo.connectioninfo.servername = "www"; logoninfo.connectionInfo .DATABASENAME = "Archives"; logoninfo.connectioninfo.userid = "sa"; logoninfo.connectioninfo.password = "123456"; oorpt.database .tables [0] .applylogonInfo (LogonInfo);
// Establish the connection between the.rpt file and the CrystalReportViewer file. CrystalReportViewer1.Reportsource = ORPT;} The report file is finally appeared! Wow, I am so happy, I can't stop standing up and stretched a lazy waist!
Step 4: The final full version of the code does not have system expansion and flexibility. The disadvantage is two: (1), the data report format file is an absolute path (2), once the settings of database access are set, in the final release is unmodable, especially the customer's SQL Server server is impossible to debug your debugging. The program environment is based on this consideration. Introducing two better things: (1), server.mappath function (2), read web.config (this example tells you how to operate the web.config configuration file) Finally modified the following: (full code) database is SQL Server2000USING CRYSTALDECISIONS.SHARED; / / is responsible for explaining the TableLogonInfo class Using CrystalDecisions.crystalReports .Engine; // Responsible to explain the ReportDocument class
private void Page_Load (object sender, System.EventArgs e) {TableLogOnInfo logOnInfo = new TableLogOnInfo (); // declare a ReportDocument where objects must first Report, while loading data report ReportDocument oRpt = new ReportDocument (); // Get File .rpt Real path string path1, path2; path1 = server.mappath ("//exer//pagelet" );path2=path1 "//crystal//cr1.rpt";/rpt.load("c://inetpub/ / wwwroot//exer//pagelet//crystal//cr1.rpt" ":orpt.load (path2);
// 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] .applylogonfo (LogonInfo);
// Establish the connection between the.RPT file and the CrystalReportViewer file. CrystalReportViewer1.reportsource = ORPT;}
Operation procedure for the entire demo: 1. In the Solution Explorer, you need to store report files, add new items: CrystalReport report, named CR1.RPT2, select data source in subsequent "Report Expert" When you do it, please select the OLE DB, then select the drive engine that responds according to your database: SQL Server: Microsoft Ole DB Provider for SQL ServerAccess: Microsoft Jet.4.03, enter the correct database connection, this step is in general Will not go wrong! 4. Establish a data report and save the CR1.rpt file 5 to open WebForm1.aspx. Add CrystalReportViewer Control: CrystalReportViewer16, add the above code to the Page_Load event in WebForm1.aspx.cs: (Note: Don't forget to quote!)
*********************************************************** *** The following is the details related to this content in Web.config
*** Note that