I query my data from database to display in my view.
I used this query :
var ien_content = from c in this.DataContext.tbl_Contents
where c.ContentTypeID == id
&&
(
IsActive == false?true :(c.Active == null?true:c.Active > 0)
)
orderby c.RegisterDate descending
select c;
return ien_content.ToList();
There are many rows in this tbl_Contents, but when all of these rows are set Active = 0, it show the error : System.NullReferenceException: Object reference not set to an instance of an object.
Anyone can tell me, how to catch this error? Thanks.
Refine your query with null check for c using where c!= null condition.
Hence you can rewrite it like this:
If still there is exception then only thing remaining is c.RegisterDate which can be null. So check, if c.Registerdate is not null for any of your rows.
Try replacing the linq with forloop so that you can debug it row by row ,something like this