hi.. i assume that you are using
MVC(razor) C#. and i'm using
LINQ
your views:
@model sample.Models.Report
@{
ViewBag.Title = "Report";
}
<script type="text/javascript">
$(document).ready(function () {
$('#btnPreview').click(function () {
var oParam = { "empCode": "" };
oParam.empCode = "001";
$.ajax({
url: '@Url.Action("SampleReport","Report")',
data: JSON.stringify(oParam),
type: 'POST',
contentType: 'application/json;',
dataType: 'json',
success: function () {
window.open('@Url.Action("showRPT", "ReportViewer")', 'mywindow', 'fullscreen=yes, scrollbars=auto');
}
});
});
});
</script>
@using (Html.BeginForm())
{
<input id = "btnPreview" type="button" value= "Preview" />
}
your model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace sample.Models
{
public class Report
{
public string field1{ get; set; }
public string field2{ get; set; }
public string field3{ get; set; }
public string field4{ get; set; }
public string field5{ get; set; }
public string field6{ get; set; }
public string field7{ get; set; }
}
}
your controller: report
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sample.ViewModel;
using sample.Models;
namespace sample.Controllers
{
public class ReportController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public void SampleReport(string empCode)
{
this.HttpContext.Session["rptName"] = "bySample.rpt";
this.HttpContext.Session["empCode"] = empCode.Trim();
this.HttpContext.Session["rptSource"] = loadRpt(empCode);
}
private List<Report> loadRpt(string param)
{
using (var db = new dbEntities())
{
var rptGen = from tblsmpl in db.tblSample
where ((tblsmpl.EmpCode.ToLower().Contains(param.ToLower().Trim())))
select tblsmpl ;
List<Report> rptList = new List<Report>();
foreach (var rptsData in rptGen)
{
rptList.Add(new Report() { field1 = rptsData.field1, field2 = rptsData.field2, field3 = rptsData.field3, field4 = rptsData.field4, field5 = rptsData.field5, field6 = rptsData.field6, field7 = rptsData.field7});
}
return (rptList);
}
}
}
}
your controller: ReportViewer
using System.Web.Mvc;
using System;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using sample.Models;
using CrystalDecisions.Web;
namespace sample.Controllers
{
public class ReportViewerController : Controller
{
public ActionResult Index()
{
return View();
}
public void showRPT()
{
try
{
bool isValid = true;
string strReportName = System.Web.HttpContext.Current.Session["rptName"].ToString();
string strempCode = System.Web.HttpContext.Current.Session["empCode"].ToString();
var rptSource = System.Web.HttpContext.Current.Session["rptSource"];
if (string.IsNullOrEmpty(strReportName))
{
isValid = false;
}
if (isValid)
{
ReportDocument rd = new ReportDocument();
string strPath = System.Web.HttpContext.Current.Server.MapPath("~/") + "rpt//" + strReportName;
rd.Load(strPath);
if (rptSource != null && rptSource.GetType().ToString() != "System.String")
rd.SetDataSource(rptSource);
if (!string.IsNullOrEmpty(empCode))
rd.SetParameterValue("empCode", strempCode);
rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
Session["rptName"] = null;
Session["empCode"] = null;
Session["rptSource"] = null;
}
else
{
Response.Write("<H2>Nothing Found; No Report name found</H2>");
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
}