Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi everyone,
i want to know how to set parameter in crystal report using mvc4
this is the view
Razor
@{
    ViewBag.Title = "Facility Booking Daily Transaction Report";
}

<h3>Facility Booking Daily Transaction Report</h3>

@using (Html.BeginForm("Report", "rptFbDailyTransacion", FormMethod.Post, new { target = "_blank" }))
{

<table>

    <tr>
    <td>
    Select Date :
    </td>
    <td>
    @Html.Telerik().DatePicker().Name("dateBook").Value(System.DateTime.Now)
    </td>
    </tr>

    <tr align="right">
        <td></td>
        <td></td>
               
        <td colspan ="5"><input type="submit" id="submit1" class = "btn" value="Show Report" />
        </td>
    </tr>

</table> 
}

this is the controller
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using PMIS.Models;

namespace PMIS.Controllers.Report
{
    public class rptFbDailyTransacionController : Controller
    {
        //
        // GET: /rptFbDailyTransacion/

        public ActionResult Index()
        {

            return View();
        }
        [HttpPost]
        public ActionResult Report(DateTime dateBook)
        {
            PMIS_DBContext db = new PMIS_DBContext();
            PMIS_DBDataSet ds = new PMIS_DBDataSet();

            System.Data.DataTable dt = ds.Tables.Add("FbdailyTransaction_Items");
            
            dt.Columns.Add("BookingNo", Type.GetType("System.String"));
            dt.Columns.Add("BookingDate", Type.GetType("System.DateTime"));
            dt.Columns.Add("FacilityType", Type.GetType("System.String"));
            dt.Columns.Add("CName", Type.GetType("System.String"));
            dt.Columns.Add("TaxAmount", Type.GetType("System.Decimal"));
            dt.Columns.Add("TotalAmount", Type.GetType("System.Decimal"));
            dt.Columns.Add("BalanceAmount", Type.GetType("System.Decimal"));
            dt.Columns.Add("UserP", Type.GetType("System.String"));
            dt.Columns.Add("Company", Type.GetType("System.String"));
            
            DataRow nr;
            var rec = from t1 in db.fbtbook1
                      join t2 in db.fbmfacilitytypes on t1.FacilityTypeId equals t2.ID
                      where t1.RecordStatus == "A" && t1.BookingDate==dateBook
                      select new
                      {
                          t1.BookingNo,
                          t1.BookingDate,
                          t2.FacilityType,
                          t1.Name,
                          t1.TaxAmount,
                          t1.TotalAmount,
                          t1.BalanceAmount
                      };
            

            string mcompany = "";

            var compRec = from mcompRec in db.mcompanies
                          select new
                          {
                              mcompRec.CompanyName
                          };

            foreach (var compRecitem in compRec)
            {
                mcompany = compRecitem.CompanyName;
            }
            
            foreach (var item in rec)
            {
                nr = dt.NewRow();
                nr["BookingNo"] = item.BookingNo;
                nr["BookingDate"] = item.BookingDate;
                nr["FacilityType"] = item.FacilityType;
                nr["CName"] = item.Name;
                nr["TaxAmount"] = item.TaxAmount;
                nr["TotalAmount"] = item.TotalAmount;
                nr["BalanceAmount"] = item.BalanceAmount;
                nr["UserP"] = User.Identity.Name.ToString();
                nr["Company"] = mcompany;

                dt.Rows.Add(nr);
            }
            Session["Reportdata"] = dt;
            Session["date"] = dateBook.ToShortDateString();
            return RedirectToAction("../ReportWebForms/rptFbDailyTransaction.aspx");
        }
    }
}

this is the WebFrom
ASP.NET
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Facility Booking Daily Transaction</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
        <CR:CrystalReportViewer ID="FbDailyTransaction"  runat="server" 
            AutoDataBind="true" />
                     
    
    </div>
    </form>
</body>
</html>

C#
<script runat="server">
    public PMIS.CrystalReports.rptFbDailyTransaction oRpt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        String strReportName = System.Web.HttpContext.Current.Session["date"].toString();

        oRpt.SetDataSource(Session["Reportdata"]);
        oRpt.ParameterFields("company",strReportName);
        FbDailyTransaction.ReportSource = oRpt;
        FbDailyTransaction.RefreshReport();        
    }
    protected void Page_Unload(object sender, EventArgs e)
    {
        if (this.oRpt != null)
        {
            this.oRpt.Close();
            this.oRpt.Dispose();
        }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        //InitializeComponent();
        base.OnInit(e);
        oRpt = new PMIS.CrystalReports.rptFbDailyTransaction();
        //GenerateReport();
    }

    private void InitializeComponent()
    {

    }
    #endregion
    
</script>


when the report load its asking parameter value
thank you
Posted
Updated 26-Jun-14 23:41pm
v2
Comments
Vishal radadiya 1-Aug-14 3:21am    
you can also check this link
http://stackoverflow.com/questions/6966593/crystal-reports-viewer-with-mvc3
hop this helpful
Vishal radadiya 1-Aug-14 3:22am    
www.youtube.com/watch?v=7yLHhsTjK68

1 solution

 
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