Click here to Skip to main content
15,444,203 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to create crystal report in MVC3?
Posted
Updated 4-Sep-12 23:55pm
v2

Try google[^].

You'll get more results (quick results) here.
 
Share this answer
 
hi.. i assume that you are using MVC(razor) C#. and i'm using LINQ

your views:
SQL
@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:
C#
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
C#
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
    {
        //
        // GET: /Report/

        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
C#
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
    {
        //
        // GET: /ReportViewer/        
        
        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");
                    
                    // Clear all sessions value
                    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());
            }
        }
     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900