I am newbie to mvc 4 and trying to generate report with asp view engine....
Following code is generating report as a image file which is getting downloaded.. but i want to display report in my view page... please help... billions of thanks in advance.....
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Report1</title>
<script src="../../Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#Button1').click(function () {
$('form').submit();
});
});
</script>
</head>
<body>
<div>
<form runat="server" id="form1">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="Submit"/>
<br />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" AsyncRendering="false" SizeToReportContent="true">
</rsweb:ReportViewer>
<br />
<img src="<%= Url.Action("Report1", "Report") %>" alt=""/>
</form>
</div>
</body>
</html>
Controller is:-
namespace Mvc4App10.Controllers
{
public class ReportController : Controller
{
public ActionResult Report1(string a)
{
return View();
}
[HttpPost]
public ActionResult Report1()
{
ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
rv.ProcessingMode = ProcessingMode.Local;
rv.LocalReport.ReportPath = Server.MapPath("~/Report1.rdlc");
DataSet1 dsCustomers = GetData("select * from Emp_Office_Details");
ReportDataSource datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]);
rv.LocalReport.DataSources.Clear();
rv.LocalReport.DataSources.Add(datasource);
rv.LocalReport.Refresh();
byte[] streamBytes = null;
string mimeType = "";
string encoding = "";
string filenameExtension = "";
string[] streamids = null;
Warning[] warnings = null;
string ReportType = "Image";
streamBytes = rv.LocalReport.Render(ReportType, null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
return File(streamBytes, mimeType, "Report1.jpeg");
}
private DataSet1 GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["EPF1Context"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet1 dsCustomers = new DataSet1())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
}
}