Click here to Skip to main content
11,571,471 members (76,427 online)
Click here to Skip to main content

Integrating Crystal Reports in .NET using C#

, 1 Jul 2006 CPOL 48.4K 2.2K 28
Rate this:
Please Sign up or sign in to vote.
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:

  2. Apply your criteria:

    //
    // 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:

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

    //
    // 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.

    //
    // 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:

    • Choose XML data source:

    • Locate file:

    • Click Finish.

This is the unformatted version of the report.

After formatting:

//
// 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)

Share

About the Author

Moutaz Salem
Web Developer
Egypt Egypt
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralNice GUI Pin
Muammar©20-Jun-09 9:12
memberMuammar©20-Jun-09 9:12 
Questioncreating a crytal report to display a dataset in the run time Pin
rkherath9-Apr-07 23:54
memberrkherath9-Apr-07 23:54 
QuestionWhy Crystal at all? Pin
Peter Wone2-Jul-06 15:53
memberPeter Wone2-Jul-06 15:53 
AnswerRe: Why Crystal at all? [modified] Pin
Michael19732-Jul-06 21:08
memberMichael19732-Jul-06 21:08 
AnswerRe: Why Crystal at all? Pin
BobWorm3-Jul-06 19:25
memberBobWorm3-Jul-06 19:25 
AnswerRe: Why Crystal at all? Pin
eatwork15-Aug-06 10:15
membereatwork15-Aug-06 10:15 
GeneralRe: Why Crystal at all? Pin
Peter Wone16-Aug-06 13:04
memberPeter Wone16-Aug-06 13:04 
GeneralRe: Why Crystal at all? Pin
M.Khadem29-Dec-06 1:21
memberM.Khadem29-Dec-06 1:21 
GeneralRe: Why Crystal at all? Pin
Peter Wone20-Jan-07 2:07
memberPeter Wone20-Jan-07 2:07 
GeneralNice! Pin
Vertyg01-Jul-06 23:09
memberVertyg01-Jul-06 23:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 2 Jul 2006
Article Copyright 2006 by Moutaz Salem
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid