Avoid double inquiry to print report crystal report c #

0

I want to raise the following question, I do not know if I am correct.

  • I generate a view in which you receive parameters to be able to generate a report and upload it as partialview the code is as follows.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ResumenProcedimientos(SearchProcedimiento model)
    {
    
        #region ResumenProcedimientos
    
        var list = from t in db.MtoProcedimientos
                   where model.itemLicitaciones.Contains(t.MtoProcedimientoId) ||
                         model.itemTipoEventos.Contains(t.MtoTipoEventoId)
                   select t;
    
        return View("_ResumenProcedimientos", list);
    
        #endregion
    }
    
  • This generates the requested information and shows me the partialview with the filter done, as I show in the following image.

    Well now I would like to print the print button to show me a report in crystal report but avoiding sending the query parameters to the printing method, is there any way?.

    What I want is to avoid going back to consult the database to be able to generate the report

    the actionresult to print is as follows.

     public ActionResult ExportPDF(int? MtoProcedimientoId, int? MtoProveedorId)
        {
            #region ExportPDF
            var reporte = this.GenerateReport(MtoProcedimientoId, MtoProveedorId, false);
            var stream = reporte.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    
            return File(stream, "application/pdf");
            #endregion
        }
    

    and this method is what generates the report

       private ReportClass GenerateReport(int? MtoProcedimientoId, int? MtoProveedorId, bool Excel)
            {
                #region GenerateReport
                var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                var connection = new SqlConnection(connectionString);
    
                var datatable = new DataTable();
    
                var sql = @"SELECT A.MtoOfertaId, E.DetalleOfertaId, D.Ejercicio, D.NoLicitacion, C.RazonSocial, C.RfcProveedor, F.Descripcion AS Estratificacion,             A.MtoProcedimientoId, A.MtoProveedorId, 
                          B.Partida, A.Gpo + ' ' + A.Gen + ' ' + A.Esp + ' ' + A.Dif + ' ' + A.[Var] AS Clave, B.Descripcion, B.Pmr, A.PrecioOfertado, A.Descuento,
                        A.PrecioNeto, A.MaximoOfertado, 
                      A.MinimoOfertado, A.MontoPartida, E.Marca, E.RegistroSanitario, E.TitularRegistro, E.RfcTitular, E.Origen, E.Fabricante, E.RfcFabricante
    FROM     dbo.MtoOferta AS A LEFT OUTER JOIN
                      dbo.MtoRequerimiento AS B ON A.MtoRequerimientoId = B.MtoRequerimientoId LEFT OUTER JOIN
                      dbo.MtoProveedor AS C ON A.MtoProveedorId = C.MtoProveedorId LEFT OUTER JOIN
                      dbo.MtoProcedimiento AS D ON B.MtoProcedimientoId = D.MtoProcedimientoId AND A.MtoProcedimientoId = D.MtoProcedimientoId LEFT OUTER JOIN
                      dbo.DetalleOferta AS E ON A.MtoOfertaId = E.MtoOfertaId LEFT OUTER JOIN
                      dbo.MtoEstratificacion AS F ON C.MtoEstratificacionId = F.MtoEstratificacionId
    GROUP BY A.MtoOfertaId, E.DetalleOfertaId, D.Ejercicio, D.NoLicitacion, C.RazonSocial, C.RfcProveedor, F.Descripcion, A.MtoProcedimientoId, A.MtoProveedorId, B.Partida, 
                      A.Gpo + ' ' + A.Gen + ' ' + A.Esp + ' ' + A.Dif + ' ' + A.[Var], B.Descripcion, B.Pmr, A.PrecioOfertado, A.Descuento, A.PrecioNeto, A.MaximoOfertado, A.MinimoOfertado, 
                      A.MontoPartida, E.Marca, E.RegistroSanitario, E.TitularRegistro, E.RfcTitular, E.Origen, E.Fabricante, E.RfcFabricante
    HAVING A.MtoProcedimientoId = " + MtoProcedimientoId;
    
                if (MtoProveedorId != 0)
                {
                    sql = sql + " and  a.MtoProveedorId=" + MtoProveedorId;
                }
    
                try
                {
                    connection.Open();
                    var command = new SqlCommand(sql, connection);
                    var adapter = new SqlDataAdapter(command);
                    adapter.Fill(datatable);
    
                }
                catch (Exception ex)
                {
    
                    ex.ToString();
                }
    
                string DirectorioReportesRelativo = "~/Reportes/Apertura/";
    
    
                var report = new ReportClass();
                if (Excel)
                {
                    //report.FileName = Server.MapPath("~/Reportes/Apertura/ValOfertasDetalle.rpt");
                    string urlArchivo = string.Format("{0}.{1}", "ValOfertasDetalle", "rpt");
    
                    string FullPathReport = string.Format("{0}{1}",
                                            this.HttpContext.Server.MapPath(DirectorioReportesRelativo),
                                             urlArchivo);
    
                    report.FileName = FullPathReport;
                }
                else
                {
                    string urlArchivo = string.Format("{0}.{1}", "ValidaOfertas", "rpt");
    
                    string FullPathReport = string.Format("{0}{1}",
                                            this.HttpContext.Server.MapPath(DirectorioReportesRelativo),
                                             urlArchivo);
    
                    report.FileName = FullPathReport;
                }
                report.Load();
    
                report.SetDataSource(datatable);
    
                return report;
                #endregion
    
            }
    

    I want to avoid the sql query to the database.

    thank you very much

        
    asked by Horacio Xochitemol 13.06.2018 в 16:33
    source

    2 answers

    0

    Certainly you could, as long as:

    • The crystal reports accept a list of objects (for the body of your report, parameters for your header or values that do not repeat themselves), take a look here , only that here they use a data table; The same depends on the crystal version you are using
    • You must save the result of the first consultation in session and then pass it to crystal
    answered by 13.06.2018 в 16:44
    0

    how thank you friend iker thank you very much I tell you how I am at the end, in the search method I made the following modifications.

    [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ResumenProcedimientos(SearchProcedimiento model)
        {
    
            #region ResumenProcedimientos
            DataTable objDataTable = new DataTable("tblSongs");
    
            objDataTable.Columns.Add("EJercicio");
            objDataTable.Columns.Add("NoLicitacion");
            objDataTable.Columns.Add("Descripcion");
            objDataTable.Columns.Add("TipoProcedimiento");
            objDataTable.Columns.Add("ClavesRequeridas");
            objDataTable.Columns.Add("PiezasRequeridas");
            objDataTable.Columns.Add("MontoRequerido");
            objDataTable.Columns.Add("ClavesAsignadas");
            objDataTable.Columns.Add("PiezasAsignadas");
            objDataTable.Columns.Add("MontoAsignado");            
            DataRow myDataRow;
    
    
            var list = from t in db.MtoProcedimientos
                       where model.itemLicitaciones.Contains(t.MtoProcedimientoId) ||
                             model.itemTipoEventos.Contains(t.MtoTipoEventoId)
                       select t;
    
            foreach (var items in list)
            {
                myDataRow = objDataTable.NewRow();
                myDataRow[0] = items.Ejercicio;
                myDataRow[1] = items.NoLicitacion;
                myDataRow[2] = items.Descripcion;
                myDataRow[3] = items.MtoTipoEvento.DescripcionCorta;                
                myDataRow[4] = items.ClavesReq;
                myDataRow[5] = items.PiezasReq;
                myDataRow[6] = items.MontoReq;
                myDataRow[7] = 0;
                myDataRow[8] = 0;
                myDataRow[9] = 0;
    
                objDataTable.Rows.Add(myDataRow);
            }
    
            Session["ResumenProcedimientos"] = objDataTable;
    
    
    
    
            return View("_ResumenProcedimientos", list);
    
            #endregion
        }
    

    now in the method to print the report

    public ActionResult ExportarResumen() 
        {
    
            if (Session["ResumenProcedimientos"] != null)
            {
                DataTable dt = (DataTable)Session["ResumenProcedimientos"];//Session["ResumenProcedimientos"] as DataTable;
    
    
                var reporte = this.GenerateReportResumen(dt);
                var stream = reporte.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                return File(stream, "application/pdf");
            }
    
            return View();
        }
    

    and the actionresult that creates the report is like this.

    private ReportClass GenerateReportResumen(DataTable table)
        {
            #region GenerateReport
    
    
            string DirectorioReportesRelativo = "~/Reportes/Procedimientos/";
    
    
            var report = new ReportClass();
            {
    
                //string urlArchivo = string.Format("{0}.{1}", "ResumenMontobyProcedimientos", "rpt");
                string urlArchivo = string.Format("{0}.{1}", "ResumenByLic", "rpt");
    
                string FullPathReport = string.Format("{0}{1}",
                                        this.HttpContext.Server.MapPath(DirectorioReportesRelativo),
                                         urlArchivo);
    
                report.FileName = FullPathReport;
            }
    
            report.Load();
    
            report.SetDataSource(table);
    
            return report;
            #endregion
    
        }
    

    the button in the view is like this.

     @Html.ActionLink("Imprimir", "ExportarResumen", null, new { @class = "fa  fa-file-pdf-o",  target = "_blank" })
    
        
    answered by 14.06.2018 в 19:31