12

I'm trying to update a single record in a table, but when I run .Firstordefault(), I get the error: "Object reference not set to an instance of an object.", and if use it with .First(), I get "Sequence contains no elements".

Using it another place, its working fine, but this time its causing errors.

Here's the code:

public class AllownceDetails
{
  public int ta_id{get;set;}
  public int tvrid{get;set;}
  public DateTime ofDate{get;set;}
  public string status{get;set;}
  public string userid {get;set;}
}
//Update Method
public void Update(AllownceDetails Allowncedtl)
    {

        var ta = (from a in ce.tbl_tvrallownce
                  where a.tvrid == Allowncedtl.tvrid 
                   //error: Sequence contains no elements

                  select a).SingleOrDefault();

        ta.status = Allowncedtl.status; 
                   //error:Object reference not set to an instance of an object
        ce.SaveChanges();


    }
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
Naveed
  • 137
  • 1
  • 1
  • 5
  • because it is returning null, thats why First() is giving error and SingleOrDefault will return null if there is no record in that case you will get Object reference not set in next statement. So make sure you got some rows in db against Allowncedt1.tvrid – Habib Mar 28 '12 at 12:58
  • This error is telling you there is no record that matches the criteria "a.tvrid == Allowncedtl.tvrid". – GTG Mar 28 '12 at 12:59
  • Possible duplicate of [Sequence contains no elements?](http://stackoverflow.com/questions/1324199/sequence-contains-no-elements) – Smit Patel Aug 02 '16 at 13:29

3 Answers3

29

The query must not be returning any data. Run a profiler on the SQL database to see the physical query being executed and try to execute it manually against the database to see what the data looks like. You probably have to adjust the query (or the data) to get the results you're looking for.

"Sequence contains no elements" is basically LINQ's way of telling you that you're trying to reference an element from a list that doesn't have anything. So calls to things like .First() or .Single() can't find anything, hence the error.

When you change the calls to something like .FirstOrDefault() or .SingleOrDefault() then it will go with "default" value for that type, and for reference types the default is null. So if you set something to null and then try to call a method on it, you'll get object reference not set to an instance of an object.

David
  • 208,112
  • 36
  • 198
  • 279
10

The Single method throws this exception when there are no elements in list(query) or there are multiple elements.

The SingleOrDefault method throws an exception when there are multiple elements in the list. Returns null when there are no elements.

The FirstOrDefault method return the first item in the list or null. There are no exceptions.

Use it and the check the reference for null

if (ta != null )
   {
      ta.status = Allowncedtl.status; 
      ce.SaveChanges()
   }

The source will always be an object, so no worries about it in your case.

Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
5

All that means is that your query isn't matching anything. Presumably Allowncedtl.tvrid is an ID which doesn't match anything in the database. You shouldn't assume that SingleOrDefault will return a non-null value. Only use SingleOrDefault when you're expecting that there may be no values - and cope with that. If a failure to find a value indicates a bug, you should use Single (or perhaps First) instead.

We can't tell anything about what the underlying cause of your error is - you should log which ID you were looking for, work out why you were looking for that, and then check it in the database.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194