I'm doing mvc reporting and i'm very new at it. I'm trying to create a report, and i have done it using rdlc. Everything works well, it can be exported to various format. My problem, when using rdlc is that we need to design and bind it first. How can i create an empty rdlc template, design and bind it with dataset programmatically.
My work so far (using empty rdlc template - just created the file without any table),
Controller File,
public ActionResult Report(string id)
{
DB.Open();
LocalReport lr1 = new LocalReport();
string path1 = Path.Combine(Server.MapPath("~/Report"), "TestEmptyReport.rdlc");
lr1.ReportPath = path1;
DataTable pc2a = new DataTable();
pc2a = DB.getDataSet().Tables[0];
pc2a.Columns.Add("Product Name");
pc2a.Columns.Add("Price");
pc2a.Columns.Add("Quantity");
ReportDataSource rdc = new ReportDataSource("DataSet1", pc2a);
lr1.DataSources.Add(rdc);
string reportType = id;
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
"<OutputFormat>" + id + "</OutputFormat>" +
"<PageWidth>8.5in</PageWidth>" +
"<PageHeight>11in</PageHeight>" +
"<MarginTop>0.5in</MarginTop>" +
"<MarginLeft>1in</MarginLeft>" +
"<MarginRight>1in</MarginRight>" +
"<MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
renderedBytes = lr1.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
return File(renderedBytes, mimeType);
}
Model File,
public DataSet getDataSet()
{
string query = "SELECT * FROM tblproduct";
if (con.State.ToString() == "Open")
{
SqlDataAdapter ad = new SqlDataAdapter(query, con);
DataSet ds = new DataSet("tblproduct");
ad.Fill(ds);
return ds;
}
else
{
return null;
}
}
View File,
<div style="padding: 10px; border: 1px solid black">
<div><a href="@Url.Action("Report", new { id = "PDF" })">Get PDF Report</a></div>
<div><a href="@Url.Action("Report", new { id = "Excel" })">Get Excel Report</a></div>
<div><a href="@Url.Action("Report", new { id = "Word" })">Get Word Report</a></div>
<div><a href="@Url.Action("Report", new { id = "Image" })">Get Image Report</a></div>
The data is there, but i just dont know how to connect it with rdlc. Means creating column based on the data and fill it with the data called from sql server.
TQVM in advanced. Explanation and example or any other method will be helpful.