Tuesday, June 12, 2012

Passing Parameter value for a rdlc report in ASP.Net and Windows Forms......

In most of the times we need to filter report data. Recently I had a requirement to create a rdlc report and add it to an ASP.Net page. In that I need to filter data using the name.

In ASP.Net and Windows forms, creating data set and reports parts are same. The different is passing the parameter value from the code.

In ASP.Net, steps used are

  • Create a Data Set by right click on the project and select Add → New Item
  • Then select Data → DataSet
  • When the DataSet is added to the solution, right click on the DataSet Designer and select Add→TableAdapter.
  • In that you need to provide the query to retrieve data and in this example I assume it as SELECT Name, Address, ContactNo FROM Users WHERE (Name LIKE @name + '%')
  • @name is the parameter which we need to pass.
  • Then add a new report and select the DataSet as the one which we created earlier.
  • Then add a ReportViewer component and select the Report as the one created earlier.
These steps are common for the Windows Forms as well. But next steps are different in ASP.Net and Windows Forms.

In ASP.Net
  • When the report is added to the report viewer, ObjectDataSource is automatically added. You need to configure it to pass the parameter.
  • Click on the arrow in the ObjectDataSource and select Configure Data Source. In the wizard you will get a step to define parameters as shown in the below figure.

  • Provide the required details as shown in the figure and click on Finish.
  • Then go to aspx.cs file and add the below code in the View button click event. Here View button is used to view the report by passing parameters. To enter parameter value a TextBox is used.

    protected void btnView_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;

            ObjectDataSource1.SelectParameters["name"].DefaultValue = name;
            ObjectDataSource1.DataBind();
            this.ReportViewer1.LocalReport.Refresh();
        }

  • Then when you click the View button, the value provided in the text box is passed as the parameter value and report data is filtered for it.
In Windows Forms
  • When the report is added to the report viewer, TableAdapter is automatically added.
  • You can pass the parameter values for the table adapter. You can pass the parameter values in the View button click event as below.

    protected void btnView_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;

            this.DataTable1TableAdapter.Fill(this.SampleDataSet.DataTable1, name);
            this.ReportViewer1.LocalReport.Refresh();
        }


  • When the View button is clicked, the parameter values are passed and data set is filtered according to those values.

5 comments:

  1. How to display the parameter that was selected/entered from an ASP textbox into a RDLC report textbox

    ReplyDelete
  2. I spent an entire day trying to get this working. Now it seems to simple. Thanks!

    ReplyDelete
  3. in vs2010 i get a error

    Object reference not set to an instance of an object.

    ReplyDelete
  4. Thanks a lot.Really helpful.Precise and Running successfully.:)

    ReplyDelete