Wednesday, 5 December 2012

Passing label value to crystal report text object in c# windows application



 private void buttonFinish_Click(object sender, EventArgs e)
        {
            string FromMonth = comboBoxFromMonth.Text, FromYear= comboBoxFromYear.Text, ToMonth= comboBoxToMonth.Text, ToYear=comboBoxToYear.Text;
            Form6 frm = new Form6(FromMonth,FromYear,ToMonth,ToYear);
            frm.MdiParent = this.MdiParent;
            frm.Dock = DockStyle.Fill;
            frm.Show();
        }














Form 6: code behind

public Form6(string FromMonth, string FromYear, string ToMonth, string ToYear)
        {
            InitializeComponent();
            label1.Text = FromMonth;
            label2.Text = FromYear;
            label3.Text = ToMonth;
            label4.Text = ToYear;
        }

 private void Form6_Load(object sender, EventArgs e)
        {
            crystalReportViewer1.DisplayGroupTree = false;
        }

private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            CrystalReport6 myreport;
            string selectSQL = null;
            selectSQL = "Select * from salaryslip where [year] between '" + label2.Text + "'AND '" + label4.Text + "' AND [month] between '" + label1.Text + "'AND'" + label3.Text + "'";
            con.Open();
            DataSet6 ds = new DataSet6();
            OleDbCommand cmd = new OleDbCommand(selectSQL, con);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(ds, "salaryslip");
            myreport = new CrystalReport6();
            myreport.SetDataSource(ds);
            crystalReportViewer1.ReportSource = myreport;

            crParameterDiscreteValue.Value = label1.Text;
            crParameterFieldDefinitions = myreport.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["FromMonth"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            crParameterDiscreteValue.Value = label2.Text;
            crParameterFieldDefinitions = myreport.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["FromYear"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            crParameterDiscreteValue.Value = label3.Text;
            crParameterFieldDefinitions = myreport.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["ToMonth"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            crParameterDiscreteValue.Value = label4.Text;
            crParameterFieldDefinitions = myreport.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["ToYear"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            da.Dispose();
            con.Close();
        }

No comments:

Post a Comment