Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everybody i have a problem in code related to print Purchases's Report between to Period .
But this Error is Shown to Me

An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Reports_reportPurchaseDate.<executeasync>b__13_0() in reportPurchaseDate.cshtml, line 73
Stack Query Cookies Headers Routing
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Reports_reportPurchaseDate.<executeasync>b__13_0() in reportPurchaseDate.cshtml
+

line 73 @item.products.ProductName

What I have tried:

my Controller
C#
public IActionResult reportPurchaseDate( DateTime datefrom, DateTime dateto)
        {
            bool isrole = getRole();
            if (isrole == true)
            {
                var rpt = _Context.orderDetails.Where(c => c.masterID.Order_Kind_ID == 1 && c.masterID.DateAdded >= datefrom && c.masterID.DateAdded <= dateto)
                    .Include(c => c.masterID)
                    .Include(c => c.products.ProductName)
                    .Include(c => c.products.quantity)
                    .Include(c => c.products.price);
                return View(rpt);

            }
            else
            {
                return RedirectToAction("noaccess", "HomeController");
            }
        }



my View
C#
@model IEnumerable<orderdetails>
@{
    ViewData["Title"] = "report purchase Date";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

HTML
<h1>تقرير مشتريات فترة</h1>
   
        <table class="table"><tbody><tr>            <td>                @Html.TextBox("datefrom")
            </td>            <td>                تاريخ بداية
            </td>        </tr>        <tr>            <td>                @Html.TextBox("dateto")
            </td>            <td>                تاريخ نهاية
            </td>        </tr>        <tr>            <td>                
            </td>            <td>            </td>        </tr>    </tbody></table>
    <hr>

    <hr>
    <div class="row" id="divprint">
        <div class="col-md-12">
            <div class="content-panel">
                <h4>
                    
                    تقرير المشتريات خلال فترة
                </h4>
                <hr>
                
                                            @foreach (var item in Model)
                        {
                                                    }


                    <table class="table"><thead>                        <tr>                            <td>                                المنتج
                            </td>                            <td>                                السعر
                            </td>                            <td>                                الكمية
                            </td>                            <td>                                تاريخ الفاتورة
                            </td>                        </tr>                    </thead>                    <tbody><tr>                                <td>                                    @item.products.ProductName
                                </td>                                <td>                                    @item.Product_Price
                                </td>                                <td>                                    @item.Product_Quantity
                                </td>                                <td>                                    @item.masterID.DateAdded.ToShortDateString()
                                </td>                            </tr></tbody>                </table>

            </div>
        </div>
    </div>

@section Scripts{
    
        function PrintElem(elem) {
            Popup($(elem).html());
        }

        function Popup(data) {
            var myWindow = window.open('', 'my div', 'height=500,width=600');
            myWindow.document.write('<html><head><title>my div</title>');
            myWindow.document.write('<link rel="stylesheet" href="/lib/bootstrap/css/bootstrap.min.css" type="text/css" />');
            myWindow.document.write('</head><body >');
            myWindow.document.write(data);
            myWindow.document.write('</body></html>');
            myWindow.document.close(); // necessary for IE >= 10

            myWindow.onload = function () { // necessary if the div contain images

                myWindow.focus(); // necessary for IE >= 10
                myWindow.print();
                myWindow.close();
            };
        }
}
Posted
Updated 26-May-20 5:23am
v5

1 solution

If you google "NullReferenceException" you will find many explanations of how to debug it. It is one of the most basic errors in .NET and one you need to understand before going any further.

In the above code either @item, products, or ProductName are null. You need to find out which one and why.
 
Share this answer
 
v2

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