TAGS :Viewed: 5 - Published at: a few seconds ago

[ how to pass combo box value as a parameter to crystal report in c# ]

i am passing combox value as parameter to crystal report but when i run my application it asks me for the value, given below. How to avoid/skip this prompt dialog and directly pass value from combobox to crystal report?

enter image description here

this is my code:

private void button2_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(Int16));
            dt.Columns.Add("month", typeof(string));

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                dt.Rows.Add(row.Cells[0].Value, row.Cells[1].Value);
            }
            ds.Tables.Add(dt);
            ds.WriteXmlSchema("sample.xml");

            CrystalReport2 rpt = new CrystalReport2();
            rpt.SetParameterValue("name",comboBox1.Text);
            rpt.SetDataSource(ds);
            crystalReportViewer1.ReportSource = rpt;
        }

Answer 1


try replacing

rpt.SetParameterValue("name",comboBox1.Text);

with

rpt.ParameterFields.Item["name"].CurrentValues = comboBox1.Text;
rpt.ParameterFields.Item["name"].HasCurrentValue = true;

if it doesn't work try this

var param= new CrystalDecisions.Shared.ParameterValues();
var paramVal = new CrystalDecisions.Shared.ParameterDiscreteValue();
paramVal.Value = comboBox1.Text;
param.Add(paramVal);
rpt.SetParameterValue("name", param)

I have adapted the code from here for you