[Solved] Blank report generated when use parameter in ASP.Net Core MVC

trisetia302
 
on Sep 10, 2021 11:34 PM
326 Views

Hi,

I tried making report generate with the ID as parameter but the result return blank value, when I use same code without the parameter the report get the correct value. Any help could be appreciate.

I'm sure sir the parameter passing not nulls value and the record on database get correct value when I tried to generate it.

The View

@using Klinik_Apps.Models
@model RawatModel

@{
    ViewBag.Title = "Report Pasien";
    Layout = "~/Views/Shared/AdminDashboard/_Layout.cshtml";
}
<div align="left">
    <table>
        <tr>
            <td>
                <input type="text" asp-for="ID_Rawat" name="ID_Rawat" class="form-control" placeholder="Enter ID Rawat"/>
            </td>
            <td>
                <form asp-action="FilterReportRawatByID" asp-controller="ReportRawat" method="post">
                    <button type="submit" class="btn btn-success btn-sm"> Export To Pdf</button>
                </form>
            </td>
        </tr>
    </table>
</div>

The Controller.cs

[HttpPost]
public IActionResult FilterReportRawatByID(int ID_Rawat)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("Db_Klinik")))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            con.Open();
            cmd.Connection = con;
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "Select * From View_Report_Join_Rawat_Pasien Where ID_Rawat=@ID_Rawat";
            cmd.Parameters.AddWithValue("@ID_Rawat", ID_Rawat);
            using (SqlDataAdapter adap = new SqlDataAdapter(cmd))
            {
                adap.Fill(dt);
            }
            con.Close();
        }
    }
    string mimtype = "";
    int extension = 1;
 
    var path = $"{this._webHostEnvirnoment.WebRootPath}\\Reporting\\ReportRawatFilterByID.rdlc";
 
    LocalReport localReport = new LocalReport(path);
    localReport.AddDataSource("DataSetRawatFilterByID", dt);
 
    Dictionary<string, string> parameters = new Dictionary<string, string>();
    parameters.Add("rp1", "Laporan Rawat Data Pasien");
 
    var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimtype);
    return File(result.MainStream, "application/pdf");
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
trisetia302
 
on Sep 11, 2021 07:23 AM

Problem Solved

The Controller.cs

        [HttpPost]
        public IActionResult FilterReportRawatByID(int ID_Rawat)
        {
            List<RawatModel> _Rawat = new List<RawatModel>();
            using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("Db_Klinik")))
            {
                using (SqlCommand cmd = new SqlCommand(null))
                {
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = "Select * From View_Report_Join_Rawat_Pasien Where ID_Rawat=@ID_Rawat";
                    cmd.Parameters.AddWithValue("@ID_Rawat", ID_Rawat);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            _Rawat.Add(new RawatModel
                            {
                                ID_Rawat = Convert.ToInt32(rdr["ID_Rawat"].ToString()),
                                ID_Pasien = Convert.ToInt32(rdr["ID_Pasien"].ToString()),
                                Nama_Pasien = rdr["Nama_Pasien"].ToString(),
                                Alamat = rdr["Alamat"].ToString(),
                                Tgl_Lahir = Convert.ToDateTime(rdr["Tgl_Lahir"].ToString()),
                                Umur = Convert.ToInt32(rdr["Umur"].ToString()),
                                Nama_Dokter = rdr["Nama_Dokter"].ToString(),
                                Tgl_Rawat = Convert.ToDateTime(rdr["Tgl_Rawat"].ToString()),
                                Nama_Penyakit = rdr["Nama_Penyakit"].ToString(),
                                Keluhan_Pasien = rdr["Keluhan_Pasien"].ToString(),
                                Hasil_Diagnosa = rdr["Hasil_Diagnosa"].ToString(),
                                Penatalaksanaan = rdr["Nama_Penyakit"].ToString()
                            });
                        }
                    }
                }
            }
            string mimtype = "";
            int extension = 1;

            var path = $"{this._webHostEnvirnoment.WebRootPath}\\Reporting\\ReportRawatFilterByID.rdlc";

            LocalReport localReport = new LocalReport(path);
            localReport.AddDataSource("DataSetRawatFilterByID", _Rawat);

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("rp1", "Data Rawat Pasien");

            var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimtype);
            return File(result.MainStream, "application/pdf");
        }