HOW TO SHOW A CRYSTAL REPORT WITHOUT USING ODBC DATA SOURCE

/*

 

 * This code demonstrates how to show a crystal report without using odbc data source.

 * It requires connection string to display report.

 * Crystal report should be made using OLE-DB Data provider.

 * Drag the crystal report viewer on form and you may set its dock property to fill.

 */

 

using System;

using System.Data.SqlClient;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

 

namespace CSharpDeveloper.Core

{

    public partial class FrmReportView : Form

    {

        public FrmReportView()

        {

            InitializeComponent();

            this.crystalReportViewer1.ReportSource = null;

        }

 

        public FrmReportView(

            ParameterFields rptParams, ReportDocument rpt, string connectionString)

            : this()

        {

            try

            {

                this.Text = rpt.FileName;

 

                if (rptParams != null)

                {

                    this.crystalReportViewer1.ParameterFieldInfo = rptParams;

                }

 

                //Load report from its location

                rpt.Load(rpt.FileName);

 

                //Set report on report viewer

                this.crystalReportViewer1.ReportSource = rpt;

 

                SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(connectionString);

                rpt.SetDatabaseLogon(

                    sb.UserID, sb.Password, sb.DataSource, sb.InitialCatalog, true);

 

                //Extract connection info from connection string           

                ConnectionInfo ci = new ConnectionInfo();

                ci.ServerName = sb.DataSource;

                ci.DatabaseName = sb.InitialCatalog;

                ci.UserID = sb.UserID;

                ci.Password = sb.Password;

 

                //Create TableLogOnInfo object and set connection info

                TableLogOnInfo tli = new TableLogOnInfo();

                tli.ConnectionInfo = ci;

 

                //Apply log on info to each table of report

                foreach (Table table in rpt.Database.Tables)

                {

                    table.ApplyLogOnInfo(tli);

                }

            }

            catch (Exception e)

            {

                MessageBox.Show(e.Message);

            }

        }

    }

}

No comments:

Post a Comment