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