-1

I am having this problem when i press a link to view data in pdf:

enter image description here

System.NullReferenceException: 'Object reference not set as an instance of an object.'

This problem arises when I try to display the SQL values ​​in PDF.

enter image description here

As you can see in the image, there is a link to see the values ​​in PDF, when you click there the error already mentioned will appear.

I share the code:

Controller:

public ActionResult Index(string buscarTitulo)
        {
            PDFPrinter db = new PDFPrinter();
            ViewBag.CurrentFilter = buscarTitulo;

            var datos = from s in db.SQLs
                        select s;
            if (!String.IsNullOrEmpty(buscarTitulo))
            {
                datos = datos.Where(s => s.Titulo.ToString().Contains(buscarTitulo.ToUpper()));
            }
            return View(datos.ToList());
        }

        public ActionResult Pdf()
        {
            var reporte = new ActionAsPdf("Index");
            return reporte;
        }

        public ActionResult Impresion(double? tit) {
            using (PDFPrinter db = new PDFPrinter()) {
                V_CuetaWeb v = db.SQLs.FirstOrDefault(c => c.Titulo == tit);
                List<V_CuetaWeb> lista = new List<V_CuetaWeb>();
                lista.Add(v);
                var reporte = new PartialViewAsPdf("Pdf", v);
                return reporte;
            }
        }

Index:

@model IEnumerable<ProvidusCuotas.V_CuetaWeb>


@{
    ViewBag.Title = "Inicio";
}
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Inicio</title>
</head>
<body>
    <form id="form">
        <div>
            @using (Html.BeginForm())
            {
                <p>
                    Título: @Html.TextBox("buscar", ViewBag.CurrentFilter as string)
                    <input type="submit" value="Filtrar" /><br />
                    <input type="button" value="Imprimir" onclick="window.print()" />
                </p>
            }
        </div>
        <div>
            <table border="1">
                @foreach (var item in Model)
                {
                    <tr>
                        <th scope="row" abbr="Suscriptor">Suscriptor: </th>
                        <td>

                            <b>@Html.DisplayFor(modelItem => item.Apellido), @Html.DisplayFor(modelItem => item.Nombre)</b>
                        </td>
                        <td>Título: @Html.DisplayFor(modelItem => item.Titulo)</td>
                    </tr>

PDF:

@model IEnumerable<ProvidusCuotas.V_CuetaWeb>

@{
    ViewBag.Title = "PDF";
}


<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Inicio</title>
</head>
<body>
    <form id="form">
        <div>
            <table border="1">
                @foreach (var item in Model)
                {
                    <tr>
                        <th scope="row" abbr="Domicilio">Domicilio: </th>
                        <td>
                            @Html.DisplayFor(modelItem => item.Domicilio)
                        </td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>Valor Nominal: @Html.DisplayFor(modelItem => item.ValNom)</td>
                    </tr>
                    <tr>
                        <th scope="row" abbr="Barrio">Barrio: </th>
                        <td>
                            @Html.DisplayFor(modelItem => item.Barrio)
                        </td>
                    </tr>
                    <tr>
                        <th scope="row" abbr="Localidad">Localidad: </th>
                        <td>
                            @Html.DisplayFor(modelItem => item.Localidad)
                        </td>
                    </tr>
                    <tr>
                        <th scope="row" abbr="Telefono">Teléfono: </th>
                        <td>
                            @Html.DisplayFor(modelItem => item.Telefono)
                        </td>
                    </tr>
                    <tr>
                        <th scope="row" abbr="Celular">Celular: </th>
                        <td>
                            @Html.DisplayFor(modelItem => item.Celular)
                        </td>
                    </tr>
                    <tr>
                        <th scope="row" abbr="Descripcion">D. Plan Actual: </th>
                        <td>
                            @Html.DisplayFor(modelItem => item.DescPlanActual)
                        </td>
                    </tr>
                    <tr>
                        <th scope="row" abbr="Fecha">Fecha Sorteo: </th>
                        <td>
                            @Html.DisplayFor(modelItem => item.FechaSorteo)
                        </td>
                    </tr>
                    <tr>
                        <th>Zona: @Html.DisplayFor(modelItem => item.acidzona)</th>
                        <th>Cobrador: @Html.DisplayFor(modelItem => item.Cobrador)</th>
                        <th>Código: @Html.DisplayFor(modelItem => item.Codigo)</th>
                        <th>Título: @Html.DisplayFor(modelItem => item.Titulo)/@Html.DisplayFor(modelItem => item.Endoso)</th>
                        <th>Sorteo: @Html.DisplayFor(modelItem => item.Sorteo)</th>
                        <th>Cuota: @Html.DisplayFor(modelItem => item.Cuota)</th>
                        <th>Vencimiento: @Html.DisplayFor(modelItem => item.Vencimiento)</th>
                        <th>Monto: @Html.DisplayFor(modelItem => item.Monto)</th>
                    </tr>
                }
            </table>
        </div>
    </form>
</body>
</html>

PDFPrinter class:

public partial class PDFPrinter : DbContext
    {
        public PDFPrinter()
            : base("name=VisorPDF")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        //public virtual DbSet<V_CuetaWeb> V_CuetaWeb { get; set; }

        public virtual DbSet<V_CuetaWeb> SQLs { get; set; }
    }

Any sugerence? how to solve this? I don't understan the mistake

  • 2
    Did you look at the stack trace? Try debugging it line by line? Which object was null? Have you read [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142) – mason Apr 13 '20 at 22:28

1 Answers1

0

I think your flow needs to change, so index will pass list of pdf objects. and it iterate from view and display a table. This part correct.

Then you click from one link, then it should pass that click object to second view. Then it will display you object values.

So pdf view model should be

@model ProvidusCuotas.V_CuetaWeb

Then no need to have foreach loop. Just access the properties and display those.

and you c# method should be like this

public ActionResult Impresion(double? tit) {
    using (PDFPrinter db = new PDFPrinter()) 
    {
        V_CuetaWeb v = db.SQLs.FirstOrDefault(c => c.Titulo == tit);
        return View("Pdf", v);
    }
}
cdev
  • 5,043
  • 2
  • 33
  • 32