Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to search in my database in a textbox there I want to type firstname and lastname and search , my code looks as following,

C#
using (var db = new knowitCVdbEntities())
           {

               var employee = (from p in db.EMPLOYEES
                               where

                                   p.firstname.Contains(TextBoxSearch.Text) &&
                                   p.lastname.Contains(TextBoxSearch.Text)


                               select p).ToList();


Doing like this I cannot search for firstname and lastname why?
Posted
Comments
bbirajdar 28-May-13 9:17am    
Because your first name and last name variables do not contain the combination of firstname and lastname
Sergey Alexandrovich Kryukov 28-May-13 9:31am    
It also can be different spelling of wrong case in the name, either in database of query.
—SA

1 solution

First & last both name should contain same string!?
:)
I think, you want one of them contain specific string
Just use Logical OR || instead of &&
C#
using (var db = new knowitCVdbEntities())
           {

               var employee = (from p in db.EMPLOYEES
                               where

                                   p.firstname.Contains(TextBoxSearch.Text) ||
                                   p.lastname.Contains(TextBoxSearch.Text)


                               select p).ToList();

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Kurac1 28-May-13 9:20am    
Hi! but I want to be able to search for both firstname and lastname not firstname or lastname ?
Aarti Meswania 28-May-13 9:26am    
are you sure you have data like that
check by fire query direct in sql

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