0

I have the following queries which are returning a Object reference not set to an instance of an object error:

    (From u In db.Customers Where u.CustomerEmail Like UserName.Text Select u.CustomerEmail).ToString
(From u In db.Customers Where u.CustomerEmail Like UserName.Text Select u).Single

If I do a sql select with the same values I get the required data:

(select CustomerEmail from dbo.Customers where dbo.CustomerEmail like @UserName) 

Please can you point out where I am going wrong with the syntax.

Thanks.

Phil
  • 1,811
  • 9
  • 38
  • 60

2 Answers2

0

What happens if you do a ToList() call after the query. Do you see the expected results? If you don't and get null back (which is incorrect as you would expect an empty IEnumerable) this would explain the exception.

Try to eliminate the used persistence framework from your code to see what happens.

thekip
  • 3,660
  • 2
  • 21
  • 41
0

Taking the first statement

(From u In db.Customers Where u.CustomerEmail Like UserName.Text Select    u.CustomerEmail).ToString

This can give a null exception if

db is null

db.Customers is null

or

UserName is null

and I think that's about it.

Does the ToString command actually execute the query? I don't know about EntityFramework, but if, like Linq2Sql, it returns the sql query, then this should not throw a null exception. If it actually executes the query, then assuming u.CustomerEmail is defined as a string, then again, I don't think this will ever throw a null exception.

If this was a case of missing data, then you would still not expect a null exception. Single might throw a 'Sequence contains no elements' error.

sgmoore
  • 15,694
  • 5
  • 43
  • 67