Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Integrating Crystal Reports in .NET using C#

Rate me:
Please Sign up or sign in to vote.
3.09/5 (13 votes)
1 Jul 2006CPOL 66.2K   2.6K   29   11
An article on integrating Crystal Reports in .NET using C#
Sample Image - main.jpg

Introduction

This article is aimed at users who want to embed Crystal reports in Windows based .NET applications using C#.

Using the Code

Embedding Crystal Reports in a Windows based application goes through easy steps:

  1. Add your report name in the reports combo:

    Image 2 Image 3
  2. Apply your criteria:

    C#
    //
    // show the controls that create the report criteria
    //
                hideAll();
                if(reportChoose.SelectedIndex==1)
                {
                    cmbCriteria.Visible=true;
                    lblCriteria1.Visible=true;
                    Choose.Size = new Size(224, 208);
                    ShowReport.Location = new Point(40, 168);
                    lblCriteria1.Text = "From Country:";
                    dbutil.fillCombo(cmbCriteria, 
    		"select country from employees group by country");
                    cmbCriteria.Items.Insert(0,"All Countries");
                    cmbCriteria.SelectedIndex = 0;
                }
                else if(reportChoose.SelectedIndex==2)
                {
                }

    So we have this:

    Image 4
  3. Create XML based on your data to create a report depending on it:

    C#
    //
    // view report action
    //
                crvReport.ReportSource = null;
                this.Cursor = Cursors.WaitCursor;
                if(reportChoose.SelectedIndex==1)
                {
                    string sql = "select * from employees";
                    if(cmbCriteria.SelectedIndex!=0)// show all employees
                    {
                        sql+= " where country='"+cmbCriteria.Text+"'";
                    }
                    WriteSchemaFile(sql);
                }
                this.Cursor = Cursors.Default;
                System.GC.Collect();

    This will create an XML file on the local drive C: (wait... it is not a fixed path report, it will be dynamic soon) called data.xml.

    C#
    //
    // WriteSchemaFile method
    //
            private void WriteSchemaFile(string SQL)
            {
                OleDbDataAdapter da = new OleDbDataAdapter(SQL, dbutil.con);
                DataSet ds = new DataSet("data");
                da.Fill(ds, "dataTable");
                ds.WriteXml("c:\\data.xml", XmlWriteMode.WriteSchema);
            }
  4. Create a report based on the XML file:

    • Add a new report:

    Image 5

    • Choose XML data source:

    Image 6

    • Locate file:

    Image 7

    • Click Finish.

This is the unformatted version of the report.
Image 8

After formatting:

Image 9

C#
//
// view report action
//
            crvReport.ReportSource = null;
            this.Cursor = Cursors.WaitCursor;
            if(reportChoose.SelectedIndex==1)
            {
                string sql = "select * from employees";
                if(cmbCriteria.SelectedIndex!=0)// show all employees
                {
                    sql+= " where country='"+cmbCriteria.Text+"'";
                }
                //WriteSchemaFile(sql);
                reports.employees emp = new CRElegantViewer.reports.employees();
                emp.Database.Tables[0].SetDataSource(getDataSet(sql));
                crvReport.ReportSource = emp;
                crvReport.Visible=true;
                tools.IsExpanded = true;
            }
            this.Cursor = Cursors.Default;
            System.GC.Collect();
//
// get DataSet Method
//
        private DataSet getDataSet(string SQL)
        {
            OleDbDataAdapter da = new OleDbDataAdapter(SQL, dbutil.con);
            DataSet ds = new DataSet("data");
            da.Fill(ds, "dataTable");
            return ds;
        }

So now, you don't need the XML file anymore.

Have fun!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPOS billing using crystal report Pin
Member 1238574617-Mar-16 2:09
Member 1238574617-Mar-16 2:09 
GeneralNice GUI Pin
Muammar©20-Jun-09 9:12
Muammar©20-Jun-09 9:12 
Questioncreating a crytal report to display a dataset in the run time Pin
rkherath9-Apr-07 23:54
rkherath9-Apr-07 23:54 
QuestionWhy Crystal at all? Pin
Peter Wone2-Jul-06 15:53
Peter Wone2-Jul-06 15:53 
The reasons for avoiding CR are legion. It is flaky, fragile, proprietary, slow, tedious to use, hideously expensive in a network licensing context and has appalling licence terms for internet use. That's now. It also contains code that is ulikely to work in next-gen OSs.

If anyone would prefer to use the SQL Reporting engine locally, just say the word and I will post an article with complete C# source demonstrating how to get the SQL Reporting engine to print directly (no preview) and work around the dotnet non-printable margin bug that afflicts it, customised print-preview dialog and anything else requested.

No royalties, and I can also show you how to dynamically put any image your code generates into the report.

PeterW
--------------------
If you can spell and use correct grammar for your compiler, what makes you think I will tolerate less?
AnswerRe: Why Crystal at all? [modified] Pin
Michael19732-Jul-06 21:08
Michael19732-Jul-06 21:08 
AnswerRe: Why Crystal at all? Pin
BobWorm3-Jul-06 19:25
BobWorm3-Jul-06 19:25 
AnswerRe: Why Crystal at all? Pin
eatwork15-Aug-06 10:15
eatwork15-Aug-06 10:15 
GeneralRe: Why Crystal at all? Pin
Peter Wone16-Aug-06 13:04
Peter Wone16-Aug-06 13:04 
GeneralRe: Why Crystal at all? Pin
M.Khadem29-Dec-06 1:21
M.Khadem29-Dec-06 1:21 
GeneralRe: Why Crystal at all? Pin
Peter Wone20-Jan-07 2:07
Peter Wone20-Jan-07 2:07 
GeneralNice! Pin
Vertyg01-Jul-06 23:09
Vertyg01-Jul-06 23:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.