I’m using ReportViewer to create some reports to my web application and I want to know:
It is possible to use ReportViewer without pre-creating the .rdlc file…the dataSet…and all the stuff?
I want to make instances of these objects, set theirs properties at runtime, to not include too much files into my application (30 reports x 3 files [dataSet, .rdlc and .aspx])
The following method explain a bit of my toughts:
protected void Page_Load(object sender, EventArgs e)
{
//getting the string connection
string connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
//estabilishing connection
using (SqlConnection conn = new SqlConnection(connString))
{
string sql = @"EXEC SP_PRODUCTS"; // or another SQL command
//opening connection
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dte = new DataTable();
//filling the dataTable with the command above
adp.Fill(dte);
//closing connection
conn.Close();
//defining which report the component will render
ReportViewer1.LocalReport.ReportPath = "myReport.rdlc";
//adding the dataSource Adicionando o data source, it's important passing the same name you defined before
//at this moment, i didn't understood if the DataSource is being created populated by the dte datatable or
//if it is just binding the dte datatable to an existing dataSource named "Products
ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("Products", dte));
//without this it wont work
ReportViewer1.DataBind();
}
}
Any idea of how to solve this?
Thanks in advance.
You have to make a rdlc file, but what you can do is to fill this rdlc with columns you need and depending on what you need you show/hide the columns. You can use parameters for this. In this way you can use only one rdlc file. You also can define an object (and make only one dataset) for all your columns you need and you populate it depending on report.