0

InquiryOrder Model

public class InquiryOrder
{
    [Key]
    public int InquiryOrderId { get; set; }

    public string InquiryOrderName { get; set; }

    [ForeignKey("ProductType")]
    public int? ProductTypeId { get; set; }
    public virtual ProductType ProductType { get; set; }
}

ProductType Model

public class ProductType
{
    [Key]
    public int ProductId { get; set; }

    [StringLength(100, ErrorMessage = "Must be less than 100 charcters", MinimumLength = 1)]
    [Required(ErrorMessage = "Required")]
    public string ProductTypeName { get; set; }

    public virtual List<InquiryOrder> inquiryOrders { get; set; }
}

InquiryOrder Controller

var displayedInquiryOrders = filteredInquiryOrders.Skip(iDisplayStart).Take(iDisplayLength);
        var result = from c in displayedInquiryOrders .AsEnumerable()
                      select new[] { 
                                Convert.ToString(c.InquiryOrderId),
                                c.InquiryOrderName,
                                c.ProductType.ProductTypeName,
                            };

Here from c.ProductType.ProductTypeName, im getting this error Object reference not set to an instance of an object. Pls help me to solve this. Thanks!

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
Isuru
  • 950
  • 1
  • 13
  • 34

2 Answers2

4

Here

new[] { 
                            Convert.ToString(c.InquiryOrderId),
                            c.InquiryOrderName,
                            c.ProductType.ProductTypeName,
                        };

Either:

  • c is null
  • c.ProductType is null
  • Thanks!. yes i have null values in table and that occurred this error. But how can i adjust this array such that it accepts null values from database? – Isuru Jan 18 '16 at 03:30
0

You need to add some NullReference controls. Pls try this:

           select new[]{ 
                            Convert.ToString(c?.InquiryOrderId),
                            c?.InquiryOrderName,
                            c?.ProductType?.ProductTypeName,
                        };