Laki
Laki

Reputation: 99

Display data using Microsoft Report Viewer

I created a windows form with Report Viewer that showing data from SQL database. What I did so far: 1. I created stored procedure on SQL server that is imported to my program (Entity Framework is used as the object mapper). 2. I created report (.rdlc) with parameter that macthes the parameter in the stored procedure. 3. Finally, I created Report Viewer and chose the report already created.

There is one parameter in the stored procedure and if I pass the value using text box everything works well. But if I try to pass a value using combo box using "Data Bound Items" Report Viewer shows nothing. This is an example of code that works (passing value by text box):

private void btnFind_Click(object sender, EventArgs e)
        {
            GetCustomer_ResultBindingSource.DataSource = db.GetCustomer(txtProductName.Text).ToList();

            ReportParameter[] rParam = new ReportParameter[]
            {
                new ReportParameter("ProductName",txtProductName.Text)
            };

            reportViewer1.LocalReport.SetParameters(rParam);
            this.reportViewer1.RefreshReport();
        }

A solution with combobox instead of textbox that doesn't show data in Report Viewer is:

    GetCustomer_ResultBindingSource.DataSource = db.GetCustomer(cboProductName.SelectedItem.ToString()).ToList();

    ReportParameter[] rParam = new ReportParameter[]
    {
            new ReportParameter("ProductName",cboProductName.SelectedItem.ToString())
    };

    reportViewer1.LocalReport.SetParameters(rParam);
    this.reportViewer1.RefreshReport();

Data source is loaded:

 private void Form1_Load(object sender, EventArgs e)
        {
            ProductBindingSource.DataSource = db.Products.ToList();
        }

Data source, Display Member and Value Member are defined in the options of the combo box (used to fill the combobox).

Any help for the second solution?

Many thanks!

Upvotes: 0

Views: 871

Answers (1)

Laki
Laki

Reputation: 99

Thanks to Henoc Salinas and his comment above. This is solved.

This is working solution:

Display Member = "Product Name" - value that is passed to SQL query

Value Member = "Product Name" (initially I put a primary key column here and it was wrong).

and therefore I called SelectedValue.ToString() (instead of SelectedItem.ToString())

The working code is:

private void btnFind_Click(object sender, EventArgs e)
        {
            GetCustomer_ResultBindingSource.DataSource = db.GetCustomer(cboProduct.SelectedValue.ToString()).ToList();

            ReportParameter[] rParam = new ReportParameter[]
            {
                new ReportParameter("ProductName", cboProduct.SelectedValue.ToString())
            };

            reportViewer1.LocalReport.SetParameters(rParam);
            reportViewer1.RefreshReport();
        }

Upvotes: 0

Related Questions