1
Project_Detail pro = new Project_Detail();
string title=Ttitle.Text;
string year1=Tyear.Text;
string key = Tkeywrds.Text;
string area = Ddl_area.Text;
string categ = Ddl_catgry.Text;
string tech = Ddl_tech.Text;
string type =Ddl_type.Text;

var q = from obj in da.Project_Details
        where obj.Project_Title.Contains(title) 
              || obj.Submission_Date.Contains(year1) 
              || obj.Keywords.Contains(key) 
              || obj.Project_Area.Contains(area) 
              || obj.Project_Category.Contains(categ) 
              || obj.Project_Technology.Contains(tech) 
              || obj.Project_Type.Contains(type)
        select obj;

if (q != null)
{
    DetailsView1.DataSource = q;
    DetailsView1.DataBind();
}
else
{
    Literal1.Text = "Data not found";
}

this code give last record of table and also not give else condition result. I want result of all condition and want to use LIke satatement.

2
  • if you want to use sql like : SqlMethods.Like(obj.parameter). Commented Apr 8, 2013 at 10:24
  • If any of the search strings is empty, your query will return all elements, as myString.Contains("") always returns true. Commented Apr 9, 2013 at 5:37

2 Answers 2

1

q is never null. It might be empty, though. So you should change your code to

if (q.Any()) // <<-----
{
    DetailsView1.DataSource = q;
    DetailsView1.DataBind();
}
else
{
    Literal1.Text = "Data not found";
}
Sign up to request clarification or add additional context in comments.

4 Comments

its not working with multiple search option.its working with one query like obj.Project_Title.Contains(title) this it is working but when have multiple condition like obj.Project_Title.Contains(title) || obj.Submission_Date.Contains(year1) || obj.Keywords.Contains(key) || obj.Project_Area.Contains(area) || obj.Project_Category.Contains(categ) || obj.Project_Technology.Contains(tech) || obj.Project_Type.Contains(type) its not working .Its give all record which in the table Please Help ...
The problem is your where clause. If you have the profiler available, get the query and examine your data.
Linq query for multiple search operation with multiple control
The problem is your logic. What happens, if any of the strings to compare is empty?
0

You can also use count method .Count()

if (q.count()>0)
{
    DetailsView1.DataSource = q;
    DetailsView1.DataBind();
}
else
{
    Literal1.Text = "Data not found";
}

1 Comment

its not working with multiple search option.its working with one query like obj.Project_Title.Contains(title) this it is working but when have multiple condition like obj.Project_Title.Contains(title) || obj.Submission_Date.Contains(year1) || obj.Keywords.Contains(key) || obj.Project_Area.Contains(area) || obj.Project_Category.Contains(categ) || obj.Project_Technology.Contains(tech) || obj.Project_Type.Contains(type) its not working .Its give all record which in the table Please Help ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.