Reports are always one of a compulsory requirements of any IT or non-IT solution. In this example we will experience how to integrate crystal report in a WPF Application.

I am using a database here to fetch some record in order to display on my report ofcourse you can use any other source. For reference my database is on SQL Server 2005, with database Ideal and I am fetching all the records in two certain columns of a table named account.

First you need to create a C# WPF Application in your visual studio 2008.

Now first you need to add a DataSet in your project. Right click your solution root and select Add then New Item. Select DataSet, stay with the default name and select ok.

Now open your DataSet1.xsd file. Right click on workspace and Select AddàData table name the table as Customer. Now add two columns in your newly created table and name them Column1 and Column2. Press CTRL+S and close DataSet1.xsd

Now right click your solution root again and select Add then New Item. This time select Crystal Report from Reporting, stay with the default name and select ok.

Now move on to Field Explorer in your report designer. Right click Database Fields and select Database Expert… and select your projects DataSet you just created. Use the following image for guidance.

clip_image002

Now right click Parameter Fields and add two new parameters and name them as col1 and col2. Like

clip_image004

Both time just enter the name and select ok.

Now Insert Parameter fields in Section 2 Page Header and Data Fields in Section 3 Details as in snap below

clip_image006

Now go to Window1.xaml and right click select view code and add references

using System.Windows.Forms;
 
 
using System.Windows.Forms.Integration;
 
 
using System.Drawing;
 
 
using System.Data;
 
 
using System.Data.SqlClient;
 
 
using CrystalDecisions.CrystalReports.Engine;
 
 
using CrystalDecisions.Windows.Forms;
 
 
using CrystalDecisions.Shared;

add two global declarations

 

CrystalReport1 objRpt;
 
CrystalReportViewer crystalReportViewer1;
 

and then write the following line of code. This one is a method to generate the query define respective names of Parameter Fields (Column Headers). This method returns the query created as string.

 

/// This method is used to
 
 
    /// 1. creat SELECT query according to the selcted 
 
    /// column names and
 
 
    /// 2. create parameters and assign values for that 
 
    /// parameter that correspond to
 
 
    /// the crystal report.
 
 
    private string CreateSelectQueryAndParameters()
 
 
    {
 
 
        ReportDocument reportDocument = new ReportDocument();
 
 
        ParameterFields paramFields = new ParameterFields();
 
 
        ParameterField paramField;
 
 
        ParameterDiscreteValue paramDiscreteValue;
 
 
        string query = "SELECT ";
 
 
        int columnNo = 0;
 
 
        columnNo++;
 
 
        query = query.Insert(query.Length, "AccountCode as Column"
 
 
        + columnNo.ToString());
 
 
        paramField = new ParameterField();
 
 
        paramField.Name = "col" + columnNo.ToString();
 
 
        paramDiscreteValue = new ParameterDiscreteValue();
 
 
        paramDiscreteValue.Value = "Account Code";
 
 
        paramField.CurrentValues.Add(paramDiscreteValue);
 
 
        //Add the paramField to paramFields
 
 
        paramFields.Add(paramField);
 
 
        columnNo++;
 
 
        query = query.Insert(query.Length, ", Name as Column"
 
 
        + columnNo.ToString());
 
 
        paramField = new ParameterField();
 
 
        paramField.Name = "col" + columnNo.ToString();
 
 
        paramDiscreteValue = new ParameterDiscreteValue();
 
 
        paramDiscreteValue.Value = "Name";
 
 
        paramField.CurrentValues.Add(paramDiscreteValue);
 
 
        //Add the paramField to paramFields
 
 
        paramFields.Add(paramField);
 
 
        query += " FROM acount";
 
 
        crystalReportViewer1.ParameterFieldInfo = paramFields;
 
 
        return query;
 
 
    }
 

Now go back to Window1.xaml and double click on window to add window loaded event in your c# code and add

 

private void Window_Loaded(object sender, RoutedEventArgs e)
 
 
    {
 
 
        WindowsFormsHost host = new WindowsFormsHost();
 
 
        crystalReportViewer1 = new CrystalReportViewer();
 
 
        objRpt = new CrystalReport1();
 
 
        string connString = @"Data Source=.;Initial Catalog
 
 
        =Ideal;Persist Security Info=True;User ID=sa";
 
 
        //Get Select query String and add parameters to the 
 
 
        //Crystal report.
 
 
        string query = CreateSelectQueryAndParameters();
 
 
        try
 
 
        {
 
 
            SqlConnection Conn = new SqlConnection(connString);
 
 
            SqlDataAdapter adepter = new SqlDataAdapter(query,
 
            connString);
 
 
            DataSet1 Ds = new DataSet1();
 
 
            adepter.Fill(Ds, "Customer");
 
 
            objRpt.SetDataSource(Ds);
 
 
            crystalReportViewer1.ReportSource = objRpt;
 
 
            host.Child = crystalReportViewer1;
 
 
            master_grid.Children.Add(host);
 
 
        }
 
 
        catch (SqlException oleEx)
 
 
        {
 
 
            System.Windows.MessageBox.Show(oleEx.Message);
 
 
        }
 
 
        catch (Exception Ex)
 
 
        {
 
 
            System.Windows.MessageBox.Show(Ex.Message);
 
 
        }
 
 
    }

Work done. Build and Execute project. 🙂

Related Stories