1

I am trying in a linq query to check if one of the fields is null but i get this error whatever i do.

"Non-static method requires a target."

This is my code :

var users = from s in db.Users
                        where s.DepartmentId == booking.Item.DepartmentId && s.UserEmail != null
                        select s;

is any way to go through this error and be able to actually check if UserEmail is null?

p.s : i am using asp.net mvc entity framework.

BPX
  • 888
  • 1
  • 8
  • 20
Wizeman1986
  • 183
  • 1
  • 4
  • 16
  • 4
    The exception you're getting comes from [trying to invoke a non-static method from a static method](https://stackoverflow.com/questions/4577191/non-static-method-requires-a-target-c-sharp). You're going to need to post more of your code. – 48klocs Feb 12 '14 at 15:46

2 Answers2

1

Be sure that the first 2 letters are uppercase (DBNull.Value).

EDIT:

Try to copy your booking item into a local variable.

var departmentId = booking.Item.DepartmentId;
var users = from s in db.Users
where s.DepartmentId == departmentId && s.UserEmail != null
select s;
Yumei De Armas
  • 386
  • 2
  • 6
  • ok i got the DBNull.Value and now i get this error : Operator '!=' cannot be applied to operands of type 'string' and 'System.DBNull' – Wizeman1986 Feb 12 '14 at 16:06
  • hmm yeah in that case, it looks like it was right as it was before (!= null). You can also try and use "!String.IsNullOrEmpty(s.UserEmail)". However, it looks like you have some other error related to your static method... (is UserEmail just a string property?) – Yumei De Armas Feb 12 '14 at 16:11
  • i tried that as well and i get the same error : "Non-static method requires a target." Yes , UserEmail is just a string property. – Wizeman1986 Feb 12 '14 at 16:20
  • And other question.. why if you remove this part? "&& s.UserEmail != null", Will it work then? (Just checking that the issue is coming from here and not somewhere else) – Yumei De Armas Feb 12 '14 at 16:31
  • Try this... var departmentId = booking.Item.DepartmentId;var users = from s in db.Users where s.DepartmentId == departmentId && s.UserEmail != null select s; – Yumei De Armas Feb 12 '14 at 16:38
  • what i got now for booking.item.DepartmentId was : Object reference not set to an instance of an object. – Wizeman1986 Feb 12 '14 at 16:50
  • LOL nice... are booking, Item and DepartmentId not null? maybe you will need to also check those cases? – Yumei De Armas Feb 12 '14 at 16:55
0
var users = from s in db.Users
     where s.DepartmentId == booking.Item.DepartmentId && s.UserEmail != DbNull.Value                                
     select s;

You need to compare it to DbNull.Value

gh9
  • 10,169
  • 10
  • 63
  • 96
  • its says DbNull does not exist in current context ? its strange because i am using the system namespace! – Wizeman1986 Feb 12 '14 at 15:53
  • ok i got the DBNull.Value and now i get this error : Operator '!=' cannot be applied to operands of type 'string' and 'System.DBNull' – Wizeman1986 Feb 12 '14 at 16:05
  • var users = from s in db.Users where s.DepartmentId == booking.Item.DepartmentId && string.IsNullOrEmpty(s.UserEmail) == false select s; – Josiane Ferice Feb 12 '14 at 17:29