Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wrote the following code to create dynamics crystal report.
I am inexperienced guy. i am not getting function of rpt.ReportDefinition.ReportObjects["emp"];.This create error that is index out of bound.

C#
using System;
using System.Data;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data.SqlClient;

namespace cbc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ReportDocument rpt = new ReportDocument();
        
        private void Form1_Load(object sender, EventArgs e)
        {
            {
                rpt.Load(@"F:\Project\cbc\cbc\CrystalReport1.rpt");
                crystalReportViewer1.ReportSource = rpt;
                crystalReportViewer1.Refresh();
            }
  

            
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            //Begin: Bind the reports dynamically
            DataTable dtReports = BindReports();
            rpt.SetDataSource(dtReports);
            //End: Bind the reports dynamically
            //Begin: Update the report label value from All to the entered employee name in the text box
            TextObject myTextObjectOnReport;
            if (rpt.ReportDefinition.ReportObjects["emp"] != null)// emp is a database table named.this is error point.
            {
                myTextObjectOnReport = (TextObject)rpt.ReportDefinition.ReportObjects["emp"];
                myTextObjectOnReport.Text = textBox7.Text;
            }
            crystalReportViewer1.ReportSource = rpt;
            crystalReportViewer1.Refresh();
            //End
           
        }

        //Filtered the report details based on the employee name entered in the textbox 
        private DataTable BindReports()
        {
            //string conString = @"Data Source =WVDI1IHCL32\WVDI1IHCL32;Initial Catalog=Test;Integrated Security=True;";
            SqlConnection con = new SqlConnection("initial catalog=sha;integrated security=yes");
            con.Open();
            int empid = Convert.ToInt32(textBox7.Text);
           // string query = "SELECT * FROM emp WHERE id="+empid+" ";
            SqlDataAdapter sqlAdapter = new SqlDataAdapter("SELECT * FROM emp WHERE id=" + textBox7.Text + " ", con);

            DataTable dtReport = new DataTable();
            sqlAdapter.Fill(dtReport);
            con.Close();

            return dtReport;
        }
Posted
Updated 6-Jul-10 20:51pm
v2

1 solution

Well, error must bebecause of missing report object that you are trying to access.

Please have a look at this article for details of how to work with crystal reports dynamically using C#:
Dynamic Crystal Report with C#[^]
 
Share this answer
 
Comments
Dreamland73 7-Jul-10 3:21am    
thanks for reply.
Please ,can you tell me about the rpt.ReportDefinition.ReportObjects["emp"];.
what will be the argument of ReportObjects[argument];

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900