[Crystal Report] Crystal Report creation and invocation method

Source: Internet
Author: User

asp.net2.0/3.5/4.0 Crystal Report creation and invocation method
1. Create Crystalreport (Add New item crystalreport in the website, or develop Crystal Report separately) and put it in the specified directory
2, use the wizard to create a report (how to create please refer to other information) Suppliers.rpt
3, vs.net2005/2008/2010 layout report, add Delete field.
4, back to the page, select the control crystalreportviewer put it on the page.
5, set the CrystalReportViewer1 data source (introduction two methods).

Introduced

Using Crystaldecisions.shared;
Using CrystalDecisions.CrystalReports.Engine;
Using Crystaldecisions.reportsource;

The first type: Pull mode

    <cr:crystalreportviewer id= "CrystalReportViewer1" runat= "Server" 
            autodatabind= "True" Grouptreeimagesfolderurl= "" height= "1202px" 
         reportsourceid= "CrystalReportSource1" Toolbarimagesfolderurl= "" 
         toolpanelwidth= "200px" width= "1104px"   toolpanelview= "None"/>
     <cr:crystalreportsource id= "CrystalReportSource1" runat= "Server" >
         < "Filename=". \.. \erp\reports\zmquotation.rpt ">
         </Report>
     </CR:CrystalReportSource>

Cs:

      CrystalReportSource1.ReportDocument.SetDatabaseLogon (Publicvar.shatdbid, PUBLICVAR.SHATDBPW, Publicvar.shatdbds, PUBLICVAR.SHATDB)//Obtain public variable
      CrystalReportSource1.ReportDocument.SetParameterValue ("@ItemP", "no");
      CrystalReportSource1.ReportDocument.SetParameterValue ("@descriptionP", "no");
      Crystalreportviewer1.databind ();

     Comments: publicvar.shatdbid username, publicvar.shatdbpw password, PUBLICVAR.SHATDBDS data source (machine name or IP), PUBLICVAR.SHATDB Database name
          CrystalReportSource1.ReportDocument.SetParameterValue ("@ItemP", "no"); Report Parameters
     This method usually needs to give a default parameter (if with parameters) for the first landing
: Push mode

        string dbconfig_sql = Publicvar.strcon;
        DataSet ds = new DataSet ();
        SqlConnection sqlcon = new SqlConnection (Dbconfig_sql);
        SqlCommand SQLCMD = new SqlCommand (sql, Sqlcon);
        SqlDataAdapter sqlad = new SqlDataAdapter ();
        Sqlad.selectcommand = SQLCMD;
        Sqlad.fill (ds, "SQL");
        If you bind the report
        //Note that you must indicate the name of the table in the dataset, you will be prompted that "the report you requested requires more information."
        CrystalReportSource1.ReportDocument.SetDataSource (ds. tables["SQL"]);
        Crystalreportviewer1.reportsource = CrystalReportSource1;
        Crystalreportviewer1.databind ();
        Unbound
        String Path = Server.MapPath ("Handqty.rpt");
        Crystalreportviewer1.load (path);
        CrystalReportSource1.ReportDocument.SetDataSource (ds. tables["SQL"]);
        Crystalreportviewer1.reportsource = CrystalReportSource1;
        Crystalreportviewer1.databind ();

Stored procedures:

        Reportdocument myreport = new Reportdocument ();
        sqlparameter[] parms = new sqlparameter[1];
        Parms[0] = new SqlParameter ("@id", SqlDbType.Int);
        Parms[0]. Value = ID; 
        DataSet ds = new DataSet ();
        ds = DataAccess.DataAccessSHDB.GetTableByStore ("project", Parms). DataSet;
 
        CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument ();
        String reportpath = Server.MapPath (".. Reports//project.rpt ");
        Doc. Load (@ "c:/test/1.rpt");
        Doc. Load (Reportpath);
        Doc. Setdatasource (ds. Tables[0]);
        Crystalreportviewer1.reportsource = doc;
        Crystalreportviewer1.databind ();

If you can't refresh the data sometimes: Add the following code

      CrystalReportSource1.ReportDocument.SetParameterValue ("@ItemP", "no");
      CrystalReportSource1.ReportDocument.SetParameterValue ("@descriptionP", "no");
      Crystalreportviewer1.databind ();


The Third Way:

Direct show on the page, does not contain Crystal Report toolbar;

private void Print () {samplerequest sr = new Samplerequest (); Sr.
        Taskcode = Taskcode;
        DataSet ds = new DataSet (); ds = Sr.
        Samplerequestdetail (); if (ds!= null) {CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new crystaldecisions.cry
            StalReports.Engine.ReportDocument (); String reportpath = Server.MapPath (".. \\..
            \\ERP\\Reports\\SampleRequest.rpt "); Doc.
            Load (Reportpath); Doc. Setdatasource (ds.

            Tables[0]);
            Crystalreportviewer1.reportsource = doc;

            Crystalreportviewer1.databind ();
            string filetype = "";
            filetype = "PDF";
            String contenttype = ""; String myfilename = Request.mappath (". \") + Session.SessionID + "." + filetype;//directory address note antisense character string Myfilen Ame = "e:\\report\\sal\\" + "Samplerequest" + baseclass.getlongdata ().
            ToString () + "." + filetype; CrystalDecisions.Shared.DiskFileDesTinationoptions mydiskoptions = new CrystalDecisions.Shared.DiskFileDestinationOptions (); Mydiskoptions.
            Diskfilename = myFileName; 
            CrystalDecisions.Shared.ExportOptions myexportoptions = new Exportoptions ();//ocr.exportoptions;
            Myexportoptions.destinationoptions = mydiskoptions;

            Myexportoptions.exportdestinationtype = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
            ContentType = "Application/pdf";

            Myexportoptions.exportformattype = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat; Doc. Export (myexportoptions);     Output report to server-side development path response.clearcontent ();
            Empty the page response.clearheaders ();
            Response.ContentType = ContentType;
            Response.WriteFile (myfilename);//write directly to the page, the page tile can achieve online reading function Response.Flush ();
        Response.close (); else {jscript.alert (no record found), this.
        Page); }
    }



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.