Reputation: 41
I am trying to pass two parameters with the input from two dropdown list from a .aspx page. in an rdlc report but in code behind it show an error.
Here is my code Below in.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Reporting.WebForms;
using System.Web.UI.WebControls;
public partial class RDLC : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
fillcontrols();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
ReportViewer1.Reset();
ReportDataSource rptsrc = new ReportDataSource("DataSet1", GetData());
ReportParameter rp1 = new ReportParameter("Month", ddlMonth.SelectedValue.ToString());
ReportParameter rp2 = new ReportParameter("Year", ddlYear.SelectedValue.ToString());
ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp1, rp2 });
ReportViewer1.LocalReport.DataSources.Add(rptsrc);
ReportViewer1.LocalReport.ReportPath = "Report.rdlc";
ReportViewer1.LocalReport.Refresh();
}
catch(Exception ex)
{ }
}
public void fillcontrols()
{
ddlMonth.Items.Clear();
ddlMonth.AppendDataBoundItems = true;
ddlMonth.Items.Add(new ListItem("Select Month", "-1"));
int nMonth = DateTime.Now.Month;
for (int j = nMonth; j <= nMonth + 10; j++)
{
ddlMonth.Items.Add(new ListItem(j.ToString(),j.ToString()));
}
ddlMonth.SelectedValue = "-1";
ddlYear.Items.Clear();
ddlYear.AppendDataBoundItems = true;
ddlYear.Items.Add(new ListItem("Select Year", "-1"));
int nYear = DateTime.Now.Year;
for (int i = nYear-3; i <= nYear + 3; i++)
{
ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlYear.SelectedValue = "-1";
}
private DataTable GetData()
{
DataTable dt = new DataTable();
string conString = System.Configuration.ConfigurationManager.ConnectionStrings["ProjectManagementConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("Select * from ProjectBilling",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
}
}
Upvotes: 0
Views: 2924
Reputation: 3729
Try this. Specify the report path and then add the datasource.
try
{
ReportViewer1.Reset();
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.DataSources.Clear();
ReportDataSource rptsrc = new ReportDataSource("DataSet1", GetData());
ReportParameter rp1 = new ReportParameter("Month", ddlMonth.SelectedValue.ToString());
ReportParameter rp2 = new ReportParameter("Year", ddlYear.SelectedValue.ToString());
ReportViewer1.LocalReport.ReportPath = "Report.rdlc";
ReportViewer1.LocalReport.DataSources.Add(rptsrc);
ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp1, rp2 }) ;
ReportViewer1.LocalReport.Refresh();
}
catch(Exception ex)
{ }
Upvotes: 2