ASP. NET web development framework 3 report development

Source: Internet
Author: User
Tags visual studio 2010
    • Bind Data Source
    • Multi-version support
    • Dynamic Parameter support

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 followingCodeTo bind data to the report to view data.

// Bind a reportReportviewer. localreport. reportpath = mappath ("Salesreport. rdlc");// The data source dataset1 must be the same as the table referenced in your report.Reportdatasource RDS =NewReportdatasource ("Salesorder", DS. Table [0]); reportviewer. localreport. CES. Add (RDS); 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:

  <  Dataset   Name = "Dataset1"  >        <  Fields  >          <  Field   Name  = "Userid"  >            <  Datafield  > Userid </  Datafield  >            <  RD: typename  > System. String </ RD: typename  >          </  Field  >          <  Field   Name  = "User_name"  >            <  Datafield  > User_name </  Datafield  >            <  RD: typename  > System. String </ RD: typename  >          </  Field  >   <  RD: datasetinfo  >             <  RD: tablename  > Aduser </  RD: tablename  > ......

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

SelectUserid, user_nameFromAduser

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

  foreach  (datatable table  in  dataset. tables) {datatable TBL = sqlhelper. executedataset (connectionstring, commandtype. text, sqls [table. tablename]). tables [0];  foreach  (datarow orow  in  TBL. rows) 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 the external resource string where the text tag is used on the interface.ProgramThe 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

Textreader RDL =Null;Using(Filestream = file. openread (PATH) {memorystream memstream =NewMemorystream (); memstream. setlength (filestream. Length); filestream. Read (memstream. getbuffer (), 0 ,(Int) Filestream. Length );// Multi-language processing 2 stands for simplified ChineseFoundation. Common. languagetranslator. languagecode = 2; RDL = reportrenderhelper. localize (filestream);} reportviewer. localreport. loadreportdefinition (RDL); 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.

<% @ Register Assembly = "Microsoft. reportviewer. webforms, version =, culture = neutral, publickeytoken = b03f5f7f11d50a3a"

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

Assembly engineassembly = assembly. Load (getlongassemblyname ("Crystaldecisions. crystalreports. Engine" , Version); Assembly sharedassembly = assembly. Load (getlongassemblyname ( "Crystaldecisions. Shared" , Version); 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

 reportparameter [] parm =  New  reportparameter [1]; parm [0] =  New  reportparameter ( "deptno" , txtdeptno. text); reportviewer. showcredentialprompts =  false ; reportviewer. processingmode = Microsoft. reporting. webforms. processingmode. remote; reportviewer. serverreport. reportserverurl =  New  system. uri ( "http: // localhost/reportserver" ); reportviewer. serverreport. reportpath =  "/enterprisesolution/salesorder" ; reportviewer. serverreport. setparameters (parm); 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, to simplify the encoding, after such processing, 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.

ObjectOBJ = reflectionhelper. getpropertyvalue (control, targetproperty );ObjectConverted = convert. changetype (OBJ, type); reflectionhelper. setpropertyvalue (entity, arr [1], converted );

use text to describe the meaning of the 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: 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.