From:
Http://forum.justdn.org/index.php?s=c29a8c789869f4b46da1ea374717a451
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
The 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
So I once again help. I all over all over the crystal report, I finally found "Access Secure Database [C #]" words, found that there is a paragraph below:
Accessing the secure database through the Crystal Reports for Visual Studio .NET is different between web forms and Windows forms. In the Windows Form, the dialog is automatically prompted to enter the username and password (the test is easy). 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 usernames and passwords, providing 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). Discovering the same report file, for Winform can show success, and for WebForm still display login failure! 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 into the table of the report. This example uses a connection to a secure SQL Server database.
Start a new project
Add a "button" and four "text box" controls to the form.
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; / / // cycle in sequence 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.pausword = 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 the Report is just a user-defined object, not the object of the system itself.
When I was confused, I suddenly thought, why not look at the Database behind the Report, what is something, finally solved, find the following information in the help
ReportDocument ORPT = New ReportDocument ();
Report belongs to the member of the DocumentCrystaldecisions.crystalReports .Engine class.
Modifying code: Adding a reference
Using crystaldecisions.shared; // Responsible for explaining TableLogonInfo class
Using crystaldecisions.crystalreports .Engine; // Responsible for explaining ReportDocument Class Private Void Page_Load (Object Sender, System.EventArgs E)
{
TablelogonInfo LogonInfo = New TablelogonInfo ();
/ / Here, a ReportDocument object REPORT must be applied in advance, and the data report is loaded.
ReportDocument ORPT = New ReportDocument ();
ORPT.LOAD ("c: //inetpub//wwwroot/ //exer/pagelet//crystal//cr1.rpt"); // Modify it to your self //'s correct position
// Establish security information
// Password-protected Microsoft Access and Paradox, etc. PC databases, but logoninfo.servername // and logoninfo.databaseename want to keep empty logoninfo.connectioninfo.servername = "www";
Logoninfo.connectioninfo.databaseName = "Archives";
Logoninfo.connectioninfo.userid = "sa";
LogonInfo.connectioninfo.password = "123456";
ORPT.DATABASE .TABLES [0] .applylogoninfo (logonfo);
// Establish a connection between the.rpt file and the CrystalReportViewer file.
CrystalReportViewer1.Reportsource = ORPT;
}
Report files have 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
The above code does not have system expansion and flexibility. There are two shortcomings:
(1), the data report format file is an absolute path
(2) Once the setting of database access is set, it is unmodable in the final release, especially the customer's SQL Server server is not like the program environment you debug.
Based on this consideration. Introducing two more good things:
(1), Server.mappath function
(2) Read web.config (this example also tells you how to operate the web.config configuration file)
The final revision is as follows: (full code) database is SQL Server2000
Using crystaldecisions.shared; // Responsible for explaining 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 ();
/ / Here, a ReportDocument object REPORT must be applied in advance, and the data report is loaded.
ReportDocument ORPT = New ReportDocument ();
// Get the true path of the.rpt file
String path1, path2;
Path1 = Server.mappath ("// EXER // Pagelet");
Path2 = Path1 "// Crystal // Cr1.rpt";
//orpt.load ("c://inetpub//wwwroot//exer/pagelet//crystal//cr1.rpt ");
ORPT.LOAD (PATH2);
/ / Get the logonInfo parameter information from Web.config
String A, B, C, D;
// Get servername
A = system.configuration .configurationSettings .appsettings ["servername"];
/ / Get DatabaseName
B = system.configuration .configurationSettings .appsettings ["database"]; // Get userid
C = system.configuration .configurationSettings .Appsettings ["userid"];
/ / Get password
D = system.configuration .configurationSettings .Appsettings ["pass"];
// Set the logonInfo parameter
Logonifo.connectioninfo.servername = a;
LogonInfo.connectioninfo.databaseName = B;
LogonInfo.connectioninfo.userid = C;
Logoninfo.connectioninfo.pa.password = d;
ORPT.DATABASE .TABLES [0] .applylogoninfo (logonfo);
// Establish a connection between the.rpt file and the CrystalReportViewer file.
CrystalReportViewer1.Reportsource = ORPT;
}
Operation procedure for the entire presentation:
1. In the Solution Explorer, you need to store the report file, add new items: CrystalReport report, named CR1.RPT
2. When selecting a data source in the Subsequent Report Experts, be sure to select OLE DB, then select the drive engine that responds according to your database:
SQL Server: Microsoft Ole DB Provider for SQL Server
Access: Microsoft Jet.4.0
3, enter the correct database connection, this step will not be wrong without anything!
4, establish a data report, and save the CR1.RPT file
5. Open WebForm1.aspx. Add CrystalReportViewer Control: Name: CrystalReportViewer1
6, add the above code in the Page_Load event in WebForm1.aspx.cs:
(Note: Don't forget to quote!)
*********************************************************** ***
Here is the details related to this content in web.config
appsettings>
*** Note that
/ / Get password
D = system.configuration .configurationSettings .Appsettings ["pass"]; here