Search code examples
c#asp.net.netreportfastreport

Create report using FastReport.Net


I have installed FastReport.Net for generating reports.

I have included following references:

FastReport.dll , FastReport.Web.dll , FastReport.Bars.dll , FastReport.Editor.dll , FastReport.Service.dll

Also I have created a custom Tab in the ToolBox called FastReport.Net and added the FastReport Controls/Components to the tab and the controls that I have are:

Pointer and WebReport

I have dragged and droppped the WebReport component on my .aspx page and this makes my web.config like this:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="FastReport.Web, Version=2016.3.13.0, Culture=neutral, PublicKeyToken=DB7E5CE63278458C" />
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="FastReport, Version=2016.3.13.0, Culture=neutral, PublicKeyToken=DB7E5CE63278458C" />
      </assemblies>
    </compilation>
  </system.web>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=WIN-SERVER;Initial Catalog=RndDatabase;User ID=development;Password=development" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport" />
    </handlers>
  </system.webServer>
</configuration>

My question is how would I be able to get the data from Database and print a report using FastReport.

I have created a dataset.xsd in my project but when I click on the Select DataSource... from the design on FastReport (ctrl+Shift+F10) it opens a pop-up with no options to select.

Is there any other procedure for this?

Edit

I have tried this:

using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

using FastReport;
using FastReport.Web.Handlers;
using FastReport.Wizards;
using FastReport.Utils;
using FastReport.TypeConverters;
using FastReport.Code;
using FastReport.MSChart;
using FastReport.Data;
using FastReport.Design.StandardDesigner;


namespace fastreports4
{
    public partial class fastrepo : System.Web.UI.Page
    {
        string sql = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public DataSet BindData()
        {
            DataSet _dataSet = new DataSet();
            SqlConnection con = new SqlConnection(sql);
            SqlCommand cmd = new SqlCommand("select * from cities", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(_dataSet);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            return _dataSet;
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet _dataSet = new DataSet();
            _dataSet = BindData();
            Report report = new Report();
            // register the "Cities" table
            report.RegisterData(_dataSet.Tables[0], "Cities");
            // enable it to use in a report
            report.GetDataSource("Cities").Enabled = true;
            // create A4 page with all margins set to 1cm
            ReportPage page1 = new ReportPage();
            page1.Name = "Page1";
            report.Pages.Add(page1);
            // create ReportTitle band
            page1.ReportTitle = new ReportTitleBand();
            page1.ReportTitle.Name = "FastReport";
            // set its height to 1.5cm
            page1.ReportTitle.Height = Units.Centimeters * 1.5f;
            // create group header
            GroupHeaderBand group1 = new GroupHeaderBand();
            group1.Name = "Cities Data";
            group1.Height = Units.Centimeters * 1;
            // set group condition
            group1.Condition = "[Cities.CityName]";//[Cities.CityName].Substring(0, 1)
            // add group to the page.Bands collection
            page1.Bands.Add(group1);
            // create group footer
            group1.GroupFooter = new GroupFooterBand();
            group1.GroupFooter.Name = "GroupFooter1";
            group1.GroupFooter.Height = Units.Centimeters * 1;
            // create DataBand
            DataBand data1 = new DataBand();
            data1.Name = "Data1";
            data1.Height = Units.Centimeters * 0.5f;
            // set data source
            data1.DataSource = report.GetDataSource("Cities");
            // connect databand to a group
            group1.Data = data1;
            // create "Text" objects
            // report title
            TextObject text1 = new TextObject();
            text1.Name = "Text1";
            // set bounds
            text1.Bounds = new System.Drawing.RectangleF(0, 0,
            Units.Centimeters * 19, Units.Centimeters * 1);
            // set text
            text1.Text = "CitiesData";
            // set appearance
            text1.HorzAlign = HorzAlign.Center;
            text1.Font = new System.Drawing.Font("Tahoma", 14, FontStyle.Bold);
            // add it to ReportTitle
            page1.ReportTitle.Objects.Add(text1);
            // group
            TextObject text2 = new TextObject();
            text2.Name = "Text2";
            text2.Bounds = new RectangleF(0, 0,
            Units.Centimeters * 2, Units.Centimeters * 1);
            text2.Text = "[Cities.CityName]";//[Cities.CityName].Substring(0, 1)
            text2.Font = new Font("Tahoma", 10, FontStyle.Bold);
            // add it to GroupHeader
            group1.Objects.Add(text2);
            report.Show();
        }
    }
}

Here I want to generate a report on a click of a button , when clicked on a Button to generate report it gives me an error on line report.Show(); saying:

DragDrop registration did not succeed.

and when I continue for execution it stops the execution at report.Show();.


Solution

  • One way is that you can use the FastReport control called WebReport.

    For this all you need to do is that create a new custom Tab in the ToolBox steps for this as follows:

    • Right click in the toolBox and click on create new tab called FastReport(you can give any name)
    • add the dlls of the FastReport
    • after this drag and drop the Webreport control and create the report.
    • If you want to interact with the database from the menu add DataSource.

    and thats it!