---
title: "how to return false if no records exist in table"  
description: "how to return false if no records exist in table"  
author: "Anonymous User"  
published: 2014-12-05  
updated: 2014-12-05  
canonical: https://www.mindstick.com/forum/12744/how-to-return-false-if-no-records-exist-in-table  
category: "asp.net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# how to return false if no records exist in table

Want to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) any [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) exist in ClientAccessCode [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap), if not return false...

```
if (!CheckAccessCodeExists()){   
Console.WriteLine("Client Access code does not exist");    throw new ConfigurationErrorsException("Client Access code does not exist");} private static bool CheckAccessCodeExists(){    using (EPOSEntities db = new EPOSEntities())    {       
ClientAccountAccess clientAccess = db.ClientAccountAccesses                .OrderByDescending(x => x.Id)               
.Take(1)               
.Single();         if(clientAccess != null)        {            return true;        }        return false;    }}
```

//this is flagging [sequence](https://www.mindstick.com/interview/34504/difference-between-identity-vs-sequence) contains no [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements), in the lamba [expression](https://www.mindstick.com/articles/1861/sqlite-expressions), so how can I just return false then? some use of .Any() perhaps?

Thanks

## Replies

### Reply by Barbara Jones

```
private static bool CheckAccessCodeExists()    {        using(EPOSEntities db = new EPOSEntities())        {            var item =db.ClientAccountAccesses.FirstOrDefault();            if(item !=null)            {              
       db.Remove(item);              
        db.SaveChanges();                return true;                             }             return false;        }    }
```

### Reply by Anonymous User

The problem is Single expects there to be at least one item in the collection, if it doesn't find 1 then it throws an exception. If it's possible for your collection to not have a record then you should be using SingleOrDefault - this will return the default value for the type you are working with, in your case this will return null.

```
ClientAccountAccess clientAccess = db.ClientAccountAccesses    .OrderByDescending(x=> x.Id)    .Take(1)  
.SingleOrDefault(); if (clientAccess != null){   
db.DeleteObject(clientAccess);}
```


---

Original Source: https://www.mindstick.com/forum/12744/how-to-return-false-if-no-records-exist-in-table

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
