ASP. NET Web development framework 3 report development

Source: Internet
Author: User

Currently, Enterprise Solution Web only supports RDLC. To support a report format, you need to do some basic work to facilitate close integration with the system.

Bind Data Source

First, let's take a look at how to use the RDLC report in the report. After designing the report file and adding the report control, we need to add the following code to bind the data to the report, to view data.

 
 
  1. // Bind a report
  2. ReportViewer. LocalReport. ReportPath = MapPath ("SalesReport. rdlc ");
  3. // The data source dataset1 must be the same as the table referenced in your report.
  4. ReportDataSource rds = new ReportDataSource ("SalesOrder", ds. Table [0]);
  5. ReportViewer. LocalReport. CES. Add (rds );
  6. ReportViewer. LocalReport. Refresh ();

This method is required for every new report to bind data to the report.

Because the Enterprise Solution system knows the location of the data source, there is a database for registration at the place where the database is registered.

Second, how to retrieve data? refer to the report definition file. The key part is as follows:

 
 
  1.  <DataSet Name="DataSet1">  
  2.       <Fields>  
  3.         <Field Name="USERID">  
  4.           <DataField>USERID</DataField>  
  5.           <rd:TypeName>System.String</rd:TypeName>  
  6.         </Field>  
  7.         <Field Name="USER_NAME">  
  8.           <DataField>USER_NAME</DataField>  
  9.           <rd:TypeName>System.String</rd:TypeName>  
  10.         </Field>  
  11.  <rd:DataSetInfo>     
  12.         <rd:TableName>ADUSER</rd:TableName>  
  13. ...... 

The key point is also here. With the help of Linq to xml, I can parse it into the following SQL statement:

 
 
  1. SELECT USERID,USER_NAME FROM ADUSER 

Return the result to the report using the Microsoft enterprise data access library, as shown in the following code:

 
 
  1. foreach (DataTable table in dataset.Tables)  
  2. {  
  3.      DataTable tbl = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, sqls  [table.TableName]).Tables[0];  
  4.  
  5.        foreach (DataRow oRow in tbl.Rows)  
  6.                 table.ImportRow(oRow);  

The automatic report value is achieved. This is a common method that can simplify a lot of C # report coding work. For example, the code at the beginning of this section can be omitted at present.

Multi-language Configuration

Second, the labels in the report interface are generally static labels, which cannot match the interface of the software system. As a result, the printed report may have different report languages than the user's offset. So far, I have learned two solutions: one is to reference external resource strings where text labels are needed on the interface, return different string resources based on program Culture. The key setting point is to reference the external string resource assembly.

For more information, see the keyword "Using Custom. NET Code with Reports.

The second method is a more reasonable method. The report definition file is in XML format. before the report is presented, you can replace the label text in the report with the appropriate string language resource. The key part of the code looks like this

 
 
  1. TextReader rdl = null;
  2. Using (FileStream fileStream = File. OpenRead (path ))
  3. {
  4. MemoryStream memStream = new MemoryStream ();
  5. MemStream. SetLength (fileStream. Length );
  6. FileStream. Read (memStream. GetBuffer (), 0, (int) fileStream. Length );
  7. // Multi-language processing 2 stands for simplified Chinese
  8. Foundation. Common. LanguageTranslator. LanguageCode = 2;
  9. Rdl = ReportRenderHelper. Localize (fileStream );
  10. }
  11. ReportViewer. LocalReport. LoadReportDefinition (rdl );
  12. ReportViewer. LocalReport. Refresh ();

Because RDLC/RDL reports support loading reports from a TextReader, you do not have to load reports from hard disk report files. Here, you can perform language conversion.

Multi-version support

Third, no matter which type of report, so far, there have been a variety of different versions. The customer's Server may also be a different version of the Server system. Microsoft has good component compatibility, but at the same time, it is also quite overbearing. For example, Visual Studio 2012 supports only Windows 7 and later operating systems, and SQL Server 2012 does not support SQL Server 2000 databases. For the ReportViewer control used here, SQL Server 2012 does not have a new version of the Control. Therefore, if you want to browse the RDLC Report designed by Report Builder 3 for SQL Server 2012, the control for rendering this report is still a component of SQL Server 2008, as shown in the following versions.

 
 
  1. <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"   
  2.     Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>   

Taking Crystal Report as an example, its runtime has many versions, and the same version has the differences between x86 and x64.

The latest version is for visual studio 2010 and the main version is 13. So many runtimes must be supported at the same time. Only reflection is used to create runtime objects of different versions based on the version installed on the client. Or, in a simple way, it is required to use the specified version of runtime. Otherwise, it is not supported.

The sample code is as follows for your reference

 
 
  1. Assembly engineAssembly = Assembly.Load(GetLongAssemblyName("CrystalDecisions.CrystalReports.Engine", version));  
  2. Assembly sharedAssembly = Assembly.Load(GetLongAssemblyName("CrystalDecisions.Shared", version));  
  3. Type printingConverterType = engineAssembly.GetType("CrystalDecisions.CrystalReports.Engine.PrintingConverter");  

Dynamic Parameter support

RDLC does not support parameter, which is supported by RDL. Microsoft explained that RDLC requires parameter processing when obtaining data. The obtained data is filtered by parameters. RDL supports parameters. When passing parameters, code snippets like this are used

 
 
  1. ReportParameter[] parm = new ReportParameter[1];  
  2. parm[0] = new ReportParameter("deptno", txtDeptno.Text);  
  3. reportViewer.ShowCredentialPrompts = false;  
  4. reportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;  
  5. reportViewer.ServerReport.ReportServerUrl = new System.Uri("http://localhost/ReportServer");  
  6. reportViewer.ServerReport.ReportPath = "/EnterpriseSolution/SalesOrder";  
  7. reportViewer.ServerReport.SetParameters(parm);  
  8. reportViewer.ServerReport.Refresh(); 

Details about parameter processing, as shown in. In the report dialog box, design parameters. A control is dynamically created during the runtime for the user to enter a value, which is then passed to the report at runtime, this process simplifies the encoding. Similarly, you do not need to write any C # code to pass the parameter value.

The field format is the format to be passed as a parameter. The data type indicates the style to be used by the control. Microsoft provides this method for type conversion.

 
 
  1. object obj = ReflectionHelper.GetPropertyValue(control, targetProperty);  
  2. object converted = Convert.ChangeType(obj, type);  
  3. ReflectionHelper.SetPropertyValue(entity, arr[1], converted); 

Use text to describe the meaning of this Code. Take TextBox as an example. The first sentence gets the Text attribute of TextBox, which is a string. If type requires a number, the second sentence converts it to a string, in the third sentence, the application reflection will pass the value to the report object. During Visual Studio debugging, strings have double quotation marks, while numbers do not. Although they all seem to be objects and their values are similar, they belong to different types. If they are not converted During computation, an error will occur.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.