0

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.

Nothing
  • 2,644
  • 11
  • 64
  • 115

3 Answers3

0

From the limited information you gave, my guess is that c itself is null. Are you sure there aren't any null rows in tbl_Contents?

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
0

Refine your query with null check for c using where c!= null condition. Hence you can rewrite it like this:

if(DataContext != null && DataContext.tbl_Contents != null)
{
    var ien_content = from c in this.DataContext.tbl_Contents
          where c!= null && c.ContentTypeID == id
          &&
          (
          IsActive == false?true :(c.Active == null?true:c.Active > 0)
          )
          orderby c.RegisterDate descending 
          select c;
}

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

List list = new List();
foreach(var c in this.DataContext.tbl_Contents)
{
if(c.ContentTypeID == id && ( IsActive == false?true :(c.Active == null?true:c.Active > 0)))
    list.Add(c)
}
Maheep
  • 5,539
  • 3
  • 28
  • 47
  • Then it must be RegisterDate which is null for some of the rows. Try removing orderby clause and see if exception still there. If no, then this is the problem. – Maheep Dec 13 '11 at 06:27
  • Umm, it still error. To me, I think the ien_content is null, so when I used "return ien_content" , It is error. But I don't know how to fix it. – Nothing Dec 13 '11 at 06:31
  • what about DataContext && DataContext.tbl_Contents? – Maheep Dec 13 '11 at 06:33
  • Try replacing the linq with forloop so that you can debug it row by row. – Maheep Dec 13 '11 at 06:38
  • I have no idea about using for loop at that place, can u give me some sample? Thanks. – Nothing Dec 13 '11 at 07:18
0

First try to test it wihout isActive line:

              var ien_content = from c in this.DataContext.tbl_Contents
              where c.ContentTypeID == id
              orderby c.RegisterDate descending 
              select c;

              if(ien_content.Count() > 0 )
              {
                //records exist
               } 
              else {//no records}

If there is no problem with that:

              var ien_content = from c in this.DataContext.tbl_Contents
              where c.ContentTypeID == id
              &&  (IsActive == false?true :(c.Active == null?true:Convert.ToInt32(c.Active) > 0))
              orderby c.RegisterDate descending 
              select c;

              if(ien_content.Count() > 0 )
              {
                return ien_content.ToList();
               } 
              else {//no records}

Or you can just seperate IsActive on your linq method.

      if(IsActive==true)
       {
 var ien_content = from c in this.DataContext.tbl_Contents
                  where c.ContentTypeID == id
                  && c.Active == null
                  orderby c.RegisterDate descending 
                  select c;

                  if(ien_content.Count() > 0 )
                  {
                     return ien_content.ToList();
                   } 
                  else {//no records}
       }
       else
       {
             var ien_content = from c in this.DataContext.tbl_Contents
              where c.ContentTypeID == id
              && Convert.ToInt32(c.Active) > 0
              orderby c.RegisterDate descending 
              select c;

              if(ien_content.Count() > 0 )
              {
                return ien_content.ToList();
               } 
              else {//no records}
        }

Regards

BizApps
  • 6,048
  • 9
  • 40
  • 62
  • Thanks, I used this if(){ return ien_content.ToList(); }else{...} So what will I return in my else block? – Nothing Dec 13 '11 at 06:51