Click here to Skip to main content
16,016,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have implemented a search on my MVC Controller. Here I am not able to search on foreign key fields. As it shows Null Reference Exception. Please find the code.

var data = from s in db.User.Include(u => u.Role) select s;
var val = from r in data.AsEnumerable() select r;

if(!String.IsNullOrEmpty(searchString))
{
data = data.Where(s => s.RowNo.ToString().ToUpper().Contains(searchString.ToUpper()) || s.UserName.ToString().ToUpper().Contains(searchString.ToUpper())|| s.RoleID.Role.ToString().ToUpper().Contains(searchstring.ToUpper())).OrderBy(x => x.UserID);
}

So the bold is foreign that I want to have search. But it is showing error like NullReference object.

I am displaying all the data of user table and RoleTable RoleName on my List View. So I have a search box and I am able 2 search on all fields apart from the Foreign key Field.

Please help me in fixing dis.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900