Am tyring to pass a parameter in a local report using a ReportViewer control in VS2010. The user clicks on a merchant, and there is a button press (not shown) which then renders the report.
I've tried using this video : How-to Pass Parameter to Report Viewer - YouTube
Problem: Code doesn't work - near xxxx at the bottom I can't figure out what should be in there
Problem: I would love to get rid of this code and use linqtosql or something simpler.
protected void Page_Load(object sender, EventArgs e)
{
DataSet1TableAdapters.MerchantNamesTableAdapter merchantNamesTableAdapter = new DataSet1TableAdapters.MerchantNamesTableAdapter();
ddlMerchants.DataSource = merchantNamesTableAdapter.GetDataAllMerchants();
ddlMerchants.DataTextField = "Name";
ddlMerchants.DataValueField = "MerchantUID";
ddlMerchants.DataBind();
ReportViewer1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
ReportViewer1.Visible = true;
var newDataSet = new DataSet();
SqlConnection sqlConnection = new SqlConnection("Data Source=.;Initial Catalog=myDataBase;Integrated Security=True");
SqlDat开发者_如何转开发aAdapter sqlDataAdapter = new SqlDataAdapter();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText = "select * from merchant where merchantUID = @MerchantUID";
sqlCommand.Parameters.AddWithValue("@MerchantUID", ddlMerchants.SelectedValue);
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(newDataSet);
ReportDataSource datasource = new ReportDataSource(xxxx, newDataSet.Tables(0));
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
ReportViewer1.LocalReport.Refresh();
}
In scenario like this I usually follow this path:
I create a dataset for the report: select the report and the menu command View - Report Data; in the Report Data windows select New ... - Dataset. I enter a name for the dataset (suppose Redemptions) and I select New ... next by DataSource, then I select Object and I navigate to my domain (or dto) class. That done I see the fields of my class and I can use it in the report;
If needed I create parameters in the Report Data Windows (right click on the Parameters Node) and define the name and type of the parameter;
I write this code to pass the data and parameters (if needed) to the report:
viewer.Reset(); ReportDataSource dataSource = new ReportDataSource(); // I use a service/repository; you could also use Linq2Sql or EntityFramework IList<Redemption> redemptions = _service.GetRedemptions(merchantId); BindingSource bindingSource = new BindingSource(redemptions, string.Empty); dataSource.Name = "Redemptions"; dataSource.Value = bindingSource; viewer.LocalReport.DataSources.Add(getDataSource(sourceInfo)); String reportName = "AD.Conso.MyReport.rdlc"; viewer.LocalReport.ReportEmbeddedResource = reportName; IList<ReportParameter> parameters = new List<ReportParameter>(); parameters.Add(new ReportParameter("myParameterName", "myParameterValue")); viewer.LocalReport.SetParameters(parameters); viewer.RefreshReport();
Note: I took this code from a project where I am using it in a slight different context, so some code could be not necessary in your context.
That xxxx is simply the name of the Datasource that you want to have. it can be anything and is used to by the constructor to Construct a named data source. Its simply an identifier . Like pass "Merchent_Redemptions" or whatever u like.
The datatable is being used as a constructor rather it should be written as an index..
Below is the code present ReportDataSource datasource = new ReportDataSource(xxxx, newDataSet.Tables(0));
The code should be in this format ReportDataSource datasource = new ReportDataSource(xxxx, newDataSet.Tables[0]);
If u have dataAdapator, DataSet then try this on form load:-
this.DataTableAdapter.Fill(this.myDatabase_DataSet.tableName, parameter01, parameter02);
this.reportViewer1.RefreshReport();
精彩评论