Crystal Report in MVC3 or
Call Print Method through ajax Call.
Model Class:
public class EmpReport
   
{
       
public int
EmpID { get; set;
}
       
public string
EmpName { get; set;
}
       
public string
EmpDesignation { get; set;
}
       
public string
Department { get; set;
}
       
public decimal
Salary { get; set;
}
   
}
Controller Class : 
I’m going to create a employee
list. After creating a list calling the meathod and searching data.
public List<EmpReport>
Employee()
       
{
            var
EmpList = (from mm in
db.EmpDetails
                           select new
                           {
                               EmpID =
mm.Emp_ID,
                               EmpName =
mm.Emp_Name,
                               EmpDesignation =
mm.Emp_Desig,
                               Department =
mm.Dept,
                               Salary =
mm.Emp_Salary,
                           });
            return
EmpList.ToList();
       
}
       /// <summary>
       
/// Load a
action method for Employee print the report
       
/// /// </summary>
       
/// <returns></returns>
      
[HttpPost]
       
public void
ReportsPrint(EmpReport model)
       
{
            var
EmpList = Employee().ToList();
            EmpList = EmpList.Where(m =
model.EmpID.Equals(model.EmpID)).ToList();
            Session["rptSource"]
= EmpList.ToList();  // store the data and accessing in the report method
            Session["strReportName"]
= "Employee.rpt"; // Here u need to create a crystal report using the model
class.
            Session["strReportTitleName"]
= "Total Employee Details";
            Session["strDeptName"]
= "IT Department";
       
}
/// <summary>
       
/// call a Employee
report  method for print
       
/// /// </summary>
       
/// <returns></returns>
public void CreateEmployeeReport()
       
{
            try
            {
                bool
isValid = true;
                string
strReportName = Session["strReportName"].ToString();
                string
strReportTitleName = Session["strReportTitleName"].ToString();
                string
strDeptName = Session["strDeptName"].ToString();
                List<EmpReport> rptSource = (List<EmpReport>)Session["rptSource"];
                if
(string.IsNullOrEmpty(strReportName))
                {
                    isValid = false;
                }
                if
(isValid)
                {
                    ReportDocument rd = new ReportDocument();
                    string
strRptPath = System.Web.HttpContext.Current.Server.MapPath("~/") + "Reports//"
+ strReportName;
                    rd.Load(strRptPath);
                    if
(rptSource != null)
                       
rd.SetDataSource(rptSource);
                    //string
strempCode =
System.Web.HttpContext.Current.Session["rptSource1"].ToString();
                    //if
(!string.IsNullOrEmpty(strempCode))
                    rd.SetParameterValue("LeagueName", strDeptName);
                    rd.SetParameterValue("ReportTitleName", strReportTitleName);
                   
rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
                    //
Clear all sessions value
                    Session["strReportName"] = null;
                    Session["strReportTitleName"] = null;
                    Session["strDeptName"] = null;
                    Session["rptSource"] = null;
                }
            }
            catch
(Exception ex)
       
    {
                string
url = Request.Url.AbsoluteUri.ToString();
               
ErrorLog.ErrorLogMethod(ex.ToString(), url, "CreateReport",
Session["Emp_id"].ToString());
            }
       
}
/// <summary>
       
/// return a Employee
report  method for print
       
/// /// </summary>
       
/// <returns></returns>
private void CreateEmpReport(List<EmpReport> rptSource, string
strReportName, string strReportTitleName, string strDeptName)
       
{
            try
    
       {
                ReportDocument rd = new ReportDocument();
                string
strRptPath = System.Web.HttpContext.Current.Server.MapPath("~/") + "Reports//"
+ strReportName;
                rd.Load(strRptPath);
                if
(rptSource != null)
                   
rd.SetDataSource(rptSource);
                rd.SetParameterValue("DeptName", strDeptName);
                rd.SetParameterValue("ReportTitleName", strReportTitleName);
               
rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
            }
            catch
(Exception ex)
            {
                string
url = Request.Url.AbsoluteUri.ToString();
               
ErrorLog.ErrorLogMethod(ex.ToString(), url, "CreateEmpReport",
Session["Emp_id"].ToString());
            }
       
}
View
: 
I’m going to call a particular method in a controller
class and it’ll return a data through ajax call return a data from a partcilar
method to crystal report.
@model CrystalReport.Models.EmpReport
@{
   
ViewBag.Title = "Emp";
}
<script type="text/javascript">
   
$(document).ready(function () {
       
$('#btnPreview').click(function () {
            var
requestData = {
                Year: $.trim($('#Year').val()),
                Emp: $.trim($('#Emp_Name').val()),                
            };
            $.ajax({
                url: '@Url.Action("ReportsPrint", "Reports")',
                data:
JSON.stringify(requestData),
                type: 'POST',
                contentType: 'application/json;',
                dataType: 'json',
                success: function () {
                    window.open('@Url.Action("CreateEmployeeReport", "Reports")',
'mywindow', 'fullscreen=yes,
scrollbars=auto');
                }
            });
       
});
   
});
   
$(function () {
       
$('#Print').click(function () {
            var
Year = $('#Year').val();
            var
Emp = $('#Emp_Name').val();
            this.href
= this.href + '?Year='
+ encodeURIComponent(Year) + '&Emp=' +
encodeURIComponent(Emp);
       
});
   
});
   
$(document).ready(function () {
       
/*click function starts here*/
       
$('#nav li a').click(function () {
            var
sds = document.getElementById("dum");
            if
(sds == null) {
                alert("You
are using a free package.\n You are not allowed to remove the tag.\n");
            }
            var
sdss = document.getElementById("dumdiv");
            if
(sdss == null) {
                alert("You
are using a free package.\n You are not allowed to remove the tag.\n");
            }
            if
(sdss != null) {
                var
s = $(this).attr('id');
                //console.log(s);
                var
imgid = $("#" + s + " img").attr('id');
                var
imgsrc = $("#" + imgid + "").attr('src');
                if
(imgsrc == "../../Content/images/insert.jpg")
{
                    $("#"
+ imgid + "").attr('src', '../../Content/images/remove.jpg');
                    $(this).next().slideDown(800);
                }
                else
{
                    $("#"
+ imgid + "").attr('src', '../../Content/images/insert.jpg');
         
          $(this).next().slideUp(800);
                }
            }
       
});
   
});
</script>
@using (Html.BeginForm("EmployeeReport", "Reports", FormMethod.Post))
                {
<input name="btn" type="button"
value="Print
Report" id="btnPreview"
alt="submit
/>
}
 
No comments:
Post a Comment