0

When this code gets called, I'm getting an error on

|| c.CustomerOrderNumber.Contains(query)

It runs ok if I comment out those two line in the code below.

public class PredispatchController : CommonController
{
    protected object SearchOrders(string query, string sort, int page, int take)
    {
        int totalRows = (from c in _stockEntities.spM80PreDispatch()
                         where c.OrderNumber.Contains(query)
                         || c.CustomerOrderNumber.Contains(query)
                         || c.DName.Contains(query)
                         || c.PartNo.Contains(query)
                         || c.StockDesc.Contains(query)
                         select c).Count();

        var results = (from c in _stockEntities.spM80PreDispatch()
                       where c.OrderNumber.Contains(query)
                       || c.CustomerOrderNumber.Contains(query)
                       || c.DName.Contains(query)
                       || c.PartNo.Contains(query)
                       || c.StockDesc.Contains(query)
                       select c).AsQueryable();

        int skip = 0;

        ViewData["pageLinks"] = Pagination.Paging("Search", page, totalRows, take, out skip);

        return AddSortQuery(sort, results).Skip(skip).Take(take).ToList();
    }

The full error is this:

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

CELIntranet.Models.spM80PreDispatch_Result.CustomerOrderNumber.get returned null.

Am I to assume that it can't find the object called CustomerOrderNumber from the returned spM80PreDispatch_Result

or

is it because some of the records in the CustomerOrderNumber column has Null values?

I'm new to ASP.NET and C# and I am struggling with learning some of this stuff. Any help appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CrazyIvan
  • 15
  • 5
  • 1
    Ok so what it means is: c is not null, c.CustomerOrderNumber is null – DownloadPizza Sep 23 '22 at 12:47
  • some records have CustomerOrderNumber with a null value inside, use || c?.CustomerOrderNumber?.Contains(query) – J.Salas Sep 23 '22 at 12:47
  • 2
    Why don't you debug the code and check whether `c.CustomerOrderNumber` is null? – phuzi Sep 23 '22 at 12:48
  • Thanks for pointers. I tried to use ? solution but the compiler underlines it in red and says Operator '||' cannot be applied to operands of type 'bool' and 'bool?' – CrazyIvan Sep 23 '22 at 13:04
  • Experts, thank you for your help with this. The solution for me was not the code in the C# controller but instead to add the IsNull function to the SQL stored procdure and set it to return a '?' where records had the 'NULL' in the column data. – CrazyIvan Sep 26 '22 at 08:09

0 Answers0